python - 如何将字符串解析为熊猫数据框
问题描述
我正在尝试构建一个独立的 Jupyter 笔记本,它将长地址字符串解析为pandas
数据帧以进行演示。目前我必须突出显示整个字符串并使用pd.read_clipboard
:
data = pd.read_clipboard(f,
comment='#',
header=None,
names=['address']).values.reshape(-1, 2)
matched_address = pd.DataFrame(data, columns=['addr_zagat', 'addr_fodor'])
我想知道是否有一种更简单的方法可以直接读取字符串,而不是依赖将某些内容复制到剪贴板。以下是字符串的前几行供参考:
f = """###################################################################################################
#
# There are 112 matches between the tuples. The Zagat tuple is listed first,
# and then its Fodors pair.
#
###################################################################################################
Arnie Morton's of Chicago 435 S. La Cienega Blvd. Los Angeles 90048 310-246-1501 Steakhouses
Arnie Morton's of Chicago 435 S. La Cienega Blvd. Los Angeles 90048 310/246-1501 American
########################
Art's Deli 12224 Ventura Blvd. Studio City 91604 818-762-1221 Delis
Art's Delicatessen 12224 Ventura Blvd. Studio City 91604 818/762-1221 American
########################
Bel-Air Hotel 701 Stone Canyon Rd. Bel Air 90077 310-472-1211 Californian
Hotel Bel-Air 701 Stone Canyon Rd. Bel Air 90077 310/472-1211 Californian
########################
Cafe Bizou 14016 Ventura Blvd. Sherman Oaks 91423 818-788-3536 French Bistro
Cafe Bizou 14016 Ventura Blvd. Sherman Oaks 91423 818/788-3536 French
########################
h Bistro
Cafe Bizou 14016 Ventura Blvd. Sherman Oaks 91423 818/788-3536 French
########################"""
有人对如何将此字符串直接解析为数据框有任何提示pandas
吗?
我意识到这里还有另一个问题可以解决:Create Pandas DataFrame from a string但该字符串由分号分隔,与我的示例中使用的格式完全不同。
解决方案
您应该添加一个示例来说明您的输出应该是什么样子,但一般来说,我会建议这样的内容:
import pandas as pd
import numpy as np
# read file, split into lines
f = open("./your_file.txt", "r").read().split('\n')
accumulator = []
# loop through lines
for line in f:
# define criteria for selecting lines
if len(line) > 1 and line[0].isupper():
# define criteria for splitting the line
# get name
first_num_char = [c for c in line if c.isdigit()][0]
name = line.split(first_num_char, 1)[0]
line = line.replace(name, '')
# get restaurant type
rest_type = line.split()[-1]
line = line.replace(rest_type, '')
# get phone number
number = line.split()[-1]
line = line.replace(number, '')
# remainder should be the address
address = line
accumulator.append([name, rest_type, number, address])
# turn accumulator into numpy array, pass with column index to DataFrame constructor
df = pd.DataFrame(np.asarray(accumulator), columns=['name', 'restaurant_type', 'phone_number', 'address'])
推荐阅读
- r - 使用 rhandsontable 转置与计数
- r - 在 R 中创建一个数据框,其中总结了所有独特的模式和它们存在的类
- reactjs - 如何使用 Top Navigator 和 Bottom Navigator 而不让 Top Navigator 与 React Navigation 中的状态栏重叠?
- php - 使用通配符将未知数量的关键字绑定到 SQL 语句
- python - 如何使用 Python 在 for 循环中将 json 对象写入和读取到谷歌云中的文件
- primefaces - 如何在过滤后更新和显示 p:dataTable 中的记录数?
- excel - 工作表声明出错
- c++ - 在 mac 命令行开发人员工具中找不到执行头 C++
- sed - 使用 sed 更新 json - Docker 容器
- android - Cordova Android 不发送 AJAX 请求