使用 pandas 将csv文件作为字典读取 [英] Reading csv file as dictionary using pandas
本文介绍了使用 pandas 将csv文件作为字典读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有傻瓜.第1行作为标题的csv:
I have the foll. csv with 1st row as header:
A B
test 23
try 34
我想以此作为字典来阅读,因此:
I want to read in this as a dictionary, so doing this:
dt = pandas.read_csv('file.csv').to_dict()
但是,这将读取标题行中的键.我希望列"A"中的值成为键.我该怎么做,即得到这样的答案:
However, this reads in the header row as key. I want the values in column 'A' to be the keys. How do I do that i.e. get answer like this:
{'test':'23', 'try':'34'}
推荐答案
dt = pandas.read_csv('file.csv', index_col=1, skiprows=1).T.to_dict()
这篇关于使用 pandas 将csv文件作为字典读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文