从字符串创建熊猫数据框(CSV格式)

时间:2018-10-24 10:09:44

标签: python pandas

我有以下格式的字符串:

"A1","B1","C1","D1","E1","F1","G1","H1"\n"A2","B2","C2","D2","E2","F2"

其中A到H是列,数字是指行。

我正在寻找创建熊猫数据框的最快方法。

我尝试过的一种很长的方法(及时完成)是使用

df = pd.DataFrame()
for row in data:
    reader = csv.reader(row)
    mylist = []
    for element in reader:
        if element!=['','']:
            mylist.append(element[0])
    df2 = pd.DataFrame([mylist])
    df = df.append(df2)

我正在寻找一种更快的方法。

1 个答案:

答案 0 :(得分:2)

我相信您需要StringIOread_csv

import pandas as pd

data = '"A1","B1","C1","D1","E1","F1","G1","H1"\n"A2","B2","C2","D2","E2","F2"'
df = pd.read_csv(pd.compat.StringIO(data), header=None)

print (df)


    0   1   2   3   4   5    6    7
0  A1  B1  C1  D1  E1  F1   G1   H1
1  A2  B2  C2  D2  E2  F2  NaN  NaN