Python Pandas DataFrame 到字典列表

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

Pandas DataFrame to List of Dictionaries

pythonlistdictionarypandasdataframe

提问by Mohamad Ibrahim

I have the following DataFrame:

我有以下数据帧:

customer    item1      item2    item3
1           apple      milk     tomato
2           water      orange   potato
3           juice      mango    chips

which I want to translate it to list of dictionaries per row

我想把它翻译成每行的字典列表

rows = [{'customer': 1, 'item1': 'apple', 'item2': 'milk', 'item3': 'tomato'},
    {'customer': 2, 'item1': 'water', 'item2': 'orange', 'item3': 'potato'},
    {'customer': 3, 'item1': 'juice', 'item2': 'mango', 'item3': 'chips'}]

采纳答案by ComputerFellow

Edit

编辑

As John Galt mentions in his answer , you should probably instead use df.to_dict('records'). It's faster than transposing manually.

正如 John Galt 在他的回答中提到的,您可能应该使用df.to_dict('records'). 它比手动移调更快。

In [20]: timeit df.T.to_dict().values()
1000 loops, best of 3: 395 μs per loop

In [21]: timeit df.to_dict('records')
10000 loops, best of 3: 53 μs per loop


Original answer

原答案

Use df.T.to_dict().values(), like below:

使用df.T.to_dict().values(),如下所示:

In [1]: df
Out[1]:
   customer  item1   item2   item3
0         1  apple    milk  tomato
1         2  water  orange  potato
2         3  juice   mango   chips

In [2]: df.T.to_dict().values()
Out[2]:
[{'customer': 1.0, 'item1': 'apple', 'item2': 'milk', 'item3': 'tomato'},
 {'customer': 2.0, 'item1': 'water', 'item2': 'orange', 'item3': 'potato'},
 {'customer': 3.0, 'item1': 'juice', 'item2': 'mango', 'item3': 'chips'}]

回答by Zero

Use df.to_dict('records')-- gives the output without having to transpose externally.

使用df.to_dict('records')-- 无需外部转置即可提供输出。

In [2]: df.to_dict('records')
Out[2]:
[{'customer': 1L, 'item1': 'apple', 'item2': 'milk', 'item3': 'tomato'},
 {'customer': 2L, 'item1': 'water', 'item2': 'orange', 'item3': 'potato'},
 {'customer': 3L, 'item1': 'juice', 'item2': 'mango', 'item3': 'chips'}]

回答by Hossain Muctadir

As an extension to John Galt'sanswer -

作为约翰高尔特答案的延伸-

For the following DataFrame,

对于以下数据帧,

   customer  item1   item2   item3
0         1  apple    milk  tomato
1         2  water  orange  potato
2         3  juice   mango   chips

If you want to get a list of dictionaries including the index values, you can do something like,

如果您想获得包含索引值的字典列表,您可以执行以下操作,

df.to_dict('index')

Which outputs a dictionary of dictionaries where keys of the parent dictionary are index values. In this particular case,

它输出一个字典字典,其中父字典的键是索引值。在这种特殊情况下,

{0: {'customer': 1, 'item1': 'apple', 'item2': 'milk', 'item3': 'tomato'},
 1: {'customer': 2, 'item1': 'water', 'item2': 'orange', 'item3': 'potato'},
 2: {'customer': 3, 'item1': 'juice', 'item2': 'mango', 'item3': 'chips'}}

回答by Joe Rivera

If you are interested in only selecting one column this will work.

如果您只对选择一列感兴趣,这将起作用。

df[["item1"]].to_dict("records")

The below will NOTwork and produces a TypeError: unsupported type: . I believe this is because it is trying to convert a series to a dict and not a Data Frame to a dict.

下面将工作,并产生一个类型错误:不支持的类型。我相信这是因为它试图将系列转换为字典,而不是将数据帧转换为字典。

df["item1"].to_dict("records")

I had a requirement to only select one column and convert it to a list of dicts with the column name as the key and was stuck on this for a bit so figured I'd share.

我需要只选择一列并将其转换为以列名作为键的字典列表,并且在此停留了一段时间,所以我想我会分享。