2017-10-08 177 views
1

我正在使用熊猫来处理数据帧。我创建了一个数据框,其行数如下:[id, vector] 其中,id是字符串类型,而vector是字典类型。Python将字典写入csv并从csv读取字典

现在,当我写一个CSV文件中的行看起来像这样(CSV文件):

25377bc2-d3b6-4699-a466-6b9f544e8ba3 {u'sport>sports event>world championship': 0.5058, u'sport>sports event': 0.7032, u'sport>soccer': 0.6377, u'lifestyle and leisure>game': 0.4673, u'sport>sports event>world cup': 0.6614, u'sport>sports event>international tournament': 0.454, u'sport>sports event>national tournament': 0.541, u'sport': 0.9069, u'sport>sports organisations>international federation': 0.5046, u'sport>sports organisations': 0.6982}  

我试着阅读从CSV回大熊猫的数据帧,但是当我看看曾经是dict它现在是<type 'str'>

我知道我可以解决它与泡菜和保存熊猫数据帧到一个泡菜文件。但是,有没有办法正确地读取CSV(其中在它的载体是型词典)

+0

是否可以保存为'json'? – jezrael

+0

是的。我会很高兴看到一个json解决方案,并且您的建议将很乐意听到 –

回答

2

我认为你可以使用json什么是更好的结构csv为节省dicts

对于写使用to_json和阅读read_json与参数orient='records',感谢piRSquared发表评论:

df = pd.DataFrame({'vector':[{'a':1, 'b':3}, {'a':4, 'b':6}], 'ID':[2,3]}) 
print (df) 
    ID   vector 
0 2 {'b': 3, 'a': 1} 
1 3 {'b': 6, 'a': 4} 

df.to_json('file.json', orient='records') 
    ID   vector 
0 2 {'b': 3, 'a': 1} 
1 3 {'b': 6, 'a': 4} 

df = pd.read_json('file.json', orient='records') 
print (df) 

print (df.applymap(type)) 
       ID   vector 
0 <class 'int'> <class 'dict'> 
1 <class 'int'> <class 'dict'> 

EDIT1:

如果是列必要的顺序相同,指数值使用:

df.to_json('file.json', orient='split') 

df = pd.read_json('file.json', orient='split') 
+0

我认为'orient ='records''可能更合适,因为生成的'json'只有'id'和'vector'键。 – piRSquared

+0

@EranMoshe - 对我来说它工作的很好,我的解决方案中存在真实数据的问题吗? – jezrael

+0

我检查了东方='记录'和没有,它都有效。谢谢。 –