繁体   English   中英

Python将字典写入csv并从csv读取字典

[英]Python writing dictionary to csv and read dictionary from csv

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

现在,当我将其写入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读回pandas数据帧,但是当我查看曾经是dict的vector的类型时,现在是<type 'str'>

我知道我可以用泡菜解决,并将该熊猫数据帧保存到泡菜文件中。 但是有没有一种方法可以正确读取csv(其中的向量是字典类型)

我认为您可以使用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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM