如何将逗号分隔的字典串拆分为Pandas数据帧

时间:2017-09-10 12:54:36

标签: python pandas

我有一个这种格式的字符串:

{apple:"34253453",oranges:"Sweet",x:"COOL"},{apple:"34222453",oranges:"Dry",x:"WARM"},{apple:"31113453",oranges:"Bitter",x:"HOT"},{apple:"38883453",oranges:"Sweet",x:"COOL"}

并希望创建一个数据框,其中列标签为“apple”,“oranges”,“x”以及各自行中的值。

我尝试使用此解决方案:Python convert comma separated list to pandas dataframe以及ast.literal_eval将其转换为列表,然后再将其转换为数据帧,但没有运气。

1 个答案:

答案 0 :(得分:2)

您的字符串无效json,因此必须首先替换:

import ast

s = '{apple:"34253453",oranges:"Sweet",x:"COOL"},{apple:"34222453",oranges:"Dry",x:"WARM"},{apple:"31113453",oranges:"Bitter",x:"HOT"},{apple:"38883453",oranges:"Sweet",x:"COOL"}'

ss = '[' + s.replace('{', '{"').replace(':"','":"').replace('",', '","') + ']'
print (ss)

[{"apple":"34253453","oranges":"Sweet","x":"COOL"},
 {"apple":"34222453","oranges":"Dry","x":"WARM"},
 {"apple":"31113453","oranges":"Bitter","x":"HOT"},
 {"apple":"38883453","oranges":"Sweet","x":"COOL"}]
df = pd.DataFrame(ast.literal_eval(ss))
print (df)
      apple oranges     x
0  34253453   Sweet  COOL
1  34222453     Dry  WARM
2  31113453  Bitter   HOT
3  38883453   Sweet  COOL
df = pd.DataFrame(pd.io.json.loads(ss))
print (df)
      apple oranges     x
0  34253453   Sweet  COOL
1  34222453     Dry  WARM
2  31113453  Bitter   HOT
3  38883453   Sweet  COOL