如何使用 Pandas 将 csv 转换为字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23057219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert csv to dictionary using pandas
提问by DevEx
How can I convert a csv into a dictionary using pandas? For example I have 2 columns, and would like column1 to be the key and column2 to be the value. My data looks like this:
如何使用 Pandas 将 csv 转换为字典?例如,我有 2 列,并且希望 column1 是键,而 column2 是值。我的数据如下所示:
"name","position"
"UCLA","73"
"SUNY","36"
cols = ['name', 'position']
df = pd.read_csv(filename, names = cols)
回答by ankostis
Since the 1st line of your sample csv-data is a "header", use this one-liner:
由于示例 csv-data 的第一行是“标题”,请使用此单行:
>>> pd.Series.from_csv(filename, header=0).to_dict()
{'UCLA': 73, 'SUNY': 36}
If you want to include also the 1st line, remove the headerkeyword (or set it to None).
如果您还想包括第一行,请删除header关键字(或将其设置为None)。
Since
pandas-0.21.0the methodSeries.from_csv()has been deprecated, and it is suggested to usepandas.read_csv()instead:>>> pd.read_csv(filename, index_col=0, squeeze=True).to_dict() {'UCLA': '73', 'SUNY': '36'}>and use this for including also the 1st header line in the dict:
>>> pd.read_csv(filename, index_col=0, squeeze=True, header=None).to_dict() {'name': 'position', 'UCLA': '73', 'SUNY': '36'}
由于
pandas-0.21.0该方法Series.from_csv()已被弃用,建议pandas.read_csv()改用:>>> pd.read_csv(filename, index_col=0, squeeze=True).to_dict() {'UCLA': '73', 'SUNY': '36'}>并使用它来包含字典中的第一个标题行:
>>> pd.read_csv(filename, index_col=0, squeeze=True, header=None).to_dict() {'name': 'position', 'UCLA': '73', 'SUNY': '36'}
回答by EdChum
Convert the columns to a list, then zip and convert to a dict:
将列转换为列表,然后压缩并转换为字典:
In [37]:
df = pd.DataFrame({'col1':['first','second','third'], 'col2':np.random.rand(3)})
print(df)
dict(zip(list(df.col1), list(df.col2)))
col1 col2
0 first 0.278247
1 second 0.459753
2 third 0.151873
[3 rows x 2 columns]
Out[37]:
{'third': 0.15187291615699894,
'first': 0.27824681093923298,
'second': 0.4597530377539677}

