Python 如何在没有索引的熊猫中将数据框转换为字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52547805/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:08:01  来源:igfitidea点击:

How to convert dataframe to dictionary in pandas WITHOUT index

pythonpandasdictionarydataframe

提问by Symphony

I have a dataframe dfas follows:

我有一个数据框df如下:

| name  | coverage |
|-------|----------|
| Jason | 25.1     |

I want to convert it to a dictionary. I used the following command in pandas:

我想把它转换成字典。我在以下命令中使用了pandas

dict=df.to_dict()

The output of dictgave me the following:

的输出dict给了我以下内容:

{'coverage': {0: 25.1}, 'name': {0: 'Jason'}} 

I do not want the 0in my output. I believe this is captured because of the column index in my dataframe df. What can I do to eliminate 0in my output ( I do not want index to be captured.) expected output :

我不希望0在我的输出中。我相信这是由于我的数据框中的列索引而捕获的df。我可以做些什么来消除0我的输出(我不希望捕获索引。)预期输出:

{'coverage': 25.1, 'name': 'Jason'} 

回答by Anton vBR

When I see your dataset with 2 columns I see a series and not a dataframe.

当我看到包含 2 列的数据集时,我看到的是一个系列而不是数据框。

Try this: d = df.set_index('name')['coverage'].to_dict()which will convert your dataframe to a series and output that.

试试这个:d = df.set_index('name')['coverage'].to_dict()这会将您的数据帧转换为一个系列并输出它。

However, if your intent is to have more columns and not a common key you could store them in an array instead using 'records'. d = df.to_dict('r'). `

但是,如果您的意图是拥有更多列而不是公共键,您可以将它们存储在一个数组中,而不是使用“记录”。d = df.to_dict('r'). `

Runnable code:

可运行代码:

import pandas as pd

df = pd.DataFrame({
    'name': ['Jason'],
    'coverage': [25.1]
})

print(df.to_dict())
print(df.set_index('name')['coverage'].to_dict())
print(df.to_dict('r'))

Returns:

返回:

{'name': {0: 'Jason'}, 'coverage': {0: 25.1}}
{'Jason': 25.1}
[{'name': 'Jason', 'coverage': 25.1}]

And one more thing, try to avoid to use variable name dict as it is reserved.

还有一件事,尽量避免使用变量名 dict ,因为它是保留的。

回答by asimo

dict1 = df.to_dict('records')

or

或者

dict2 = df.to_dict('list')

list: keys are column names, values are lists of column data

list: 键是列名,值是列数据列表

records: each row becomes a dictionary where key is column name and value is the data in the cell

records: 每行变成一个字典,其中键是列名,值是单元格中的数据