将python dict或csv文件转换为数据帧

时间:2015-11-17 03:24:51

标签: python numpy dictionary dataframe

我有一个字典转储为csv,如下所示。我如何将其转换为数据帧 X,Y,SN作为标题?我正在考虑编写for循环并使用split 但它似乎并不优雅也没有效率......

"49,42",001C0BA79A44
"49,43",001C0BA79A46
"49,40",001C0BA79A40
"49,41",001C0BA79A42
"67,22",001C0BA791E8
"65,31",001C0BA7991E
"67,23",001C0BA793BA
"64,25",001C0BA79416
"74,29",001C0BA79354
"65,33",001C0BA79922
"58,12",001C0BA78B2D
"65,32",001C0BA79920
"72,36",001C0BA79E08
"49,31",001C0BA79525
"65,51",001C0BA7A834
"62,46",001C0BA7A729
"49,30",001C0BA79523
"66,40",001C0BA79F45
"52,42",001C0BA7A07E
"58,17",001C0BA78F49
"52,40",001C0BA79A5E
"52,41",001C0BA79A60
"52,46",001C0BA7A0B8
"52,44",001C0BA7A082
"64,20",001C0BA791AD
"55,29",001C0BA7963E
"55,28",001C0BA7963C
"69,48",001C0BA7A689
"58,15",001C0BA78F45
"55,21",001C0BA79136
"55,20",001C0BA79134
"55,23",001C0BA7913A
"55,22",001C0BA79138
"55,25",001C0BA794A1
"55,24",001C0BA7949F

1 个答案:

答案 0 :(得分:1)

不要使用for循环,向现有数据框添加行效率不高。

def split(string):
     return string.split(',', 1)

df = pd.read_csv(file_path, header=None, names=['Cord', 'SN'])
df['X'], df['Y'] = zip(*df.Cord.map(split))
df = df[['X', 'Y', 'SN']]

请参阅文档here