为了演示,我正在尝试构建一个自包含的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但是字符串由分号分隔,与我的示例中使用的格式完全不同。你知道吗
您应该添加一个示例,说明您的输出应该是什么样子的,但一般来说,我建议如下:
相关问题 更多 >
编程相关推荐