【发布时间】:2017-10-23 08:31:57
【问题描述】:
我还是 Pandas 的新手。是否可以在循环线路时启动并附加到 Pandas 数据帧?我的尝试如下,但它创建了一个包含 1 列而不是 6 列的数据框。将修改后的输入保存到 csv 文件然后用 Pandas 读取该 csv 文件会更容易吗?我现在可能会这样做。谢谢!
import requests
import pandas as pd
url = 'https://raw.githubusercontent.com/23andMe/yhaplo/master/input/isogg.2016.01.04.txt'
r = requests.get(url)
for i, line in enumerate(r.text.splitlines()):
l = line.strip().split('\t')
## The header is on the first line.
if i == 0:
df = pd.DataFrame([s.strip() for s in l])
## Lines with 6 columns.
elif len(l) == 6:
df = df.append(pd.DataFrame([s.strip() for s in l]))
## Lines with 7 columns.
elif len(l) == 7:
df = df.append(pd.DataFrame([l[i].strip() for i in (0, 2, 3, 4, 5, 6)]))
【问题讨论】:
标签: python python-3.x pandas dataframe append