将 pandas.DataFrame 转换为 Python 中的字典列表

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

Convert pandas.DataFrame to list of dictionaries in Python

pythonjsonpandasdictionarydataframe

提问by Shankar Pandey

I have a dictionary which is converted from a dataframe as below :

我有一个从数据帧转换而来的字典,如下所示:

a = d.to_json(orient='index')

Dictionary :

字典:

{"0":{"yr":2017,"PKID":"58306, 57011","Subject":"ABC","ID":"T001"},"1":{"yr":2018,"PKID":"1234,54321","Subject":"XYZ","ID":"T002"}}

What I need is it be in a list, so essentially a list of dictionary. So i just add a [] because that is the format to be used in the rest of the code.

我需要的是它在一个列表中,所以本质上是一个字典列表。所以我只添加一个 [] 因为这是在其余代码中使用的格式。

input_dict = [a]

input_dict :

输入字典:

['
{"0":{"yr":2017,"PKID":"58306, 57011","Subject":"ABC","ID":"T001"},"1":{"yr":2018,"PKID":"1234,54321","Subject":"XYZ","ID":"T002"}}
']

I need to get the single quotes removed just after the [ and just before the ]. Also, have the PKID values in form of list.

我需要在 [ 之后和 ] 之前删除单引号。此外,以列表的形式获取 PKID 值。

How can this be achieved ?

如何做到这一点?

Expected Output :

预期输出:

[ {"yr":2017,"PKID":[58306, 57011],"Subject":"ABC","ID":"T001"},"1":{"yr":2018,"PKID":[1234,54321],"Subject":"XYZ","ID":"T002"} ]

NOTE : The PKID column has multiple integer values which have to come as a lift of integers. a string is not acceptable. so we need like "PKID":[58306, 57011] and not "PKID":"[58306, 57011]"

注意:PKID 列有多个整数值,它们必须是整数的提升。字符串是不可接受的。所以我们需要像 "PKID":[58306, 57011] 而不是 "PKID":"[58306, 57011]"

回答by Norrius

pandas.DataFrame.to_jsonreturns a string (JSON string), not a dictionary. Try to_dictinstead:

pandas.DataFrame.to_json返回一个字符串(JSON 字符串),而不是一个字典。试试吧to_dict

>>> df
   col1  col2
0     1     3
1     2     4
>>> [df.to_dict(orient='index')]
[{0: {'col1': 1, 'col2': 3}, 1: {'col1': 2, 'col2': 4}}]
>>> df.to_dict(orient='records')
[{'col1': 1, 'col2': 3}, {'col1': 2, 'col2': 4}]

回答by jpp

Here is one way:

这是一种方法:

from collections import OrderedDict

d = {"0":{"yr":2017,"PKID":"58306, 57011","Subject":"ABC","ID":"T001"},"1":{"yr":2018,"PKID":"1234,54321","Subject":"XYZ","ID":"T002"}}

list(OrderedDict(sorted(d.items())).values())

# [{'ID': 'T001', 'PKID': '58306, 57011', 'Subject': 'ABC', 'yr': 2017},
#  {'ID': 'T002', 'PKID': '1234,54321', 'Subject': 'XYZ', 'yr': 2018}]

Note the ordered dictionary is ordered by text string keys, as supplied. You may wish to convert these to integers first before any processing via d = {int(k): v for k, v in d.items()}.

请注意,有序字典按提供的文本字符串键排序。您可能希望在通过d = {int(k): v for k, v in d.items()}.

回答by Ryan

You are converting your dictionary to jsonwhich is a string. Then you wrap your resulting string a list. So, naturally, the result is a string inside of a list.

您正在将字典转换json为字符串。然后将结果字符串包装成一个列表。因此,自然地,结果是列表中的字符串。

Try instead: [d]where dis your raw dictionary (not converted json

试试看:你的原始字典[d]在哪里d(未转换json

回答by Rakesh

You can use a list comprehension

您可以使用列表理解

Ex:

前任:

d = {"0":{"yr":2017,"PKID":"58306, 57011","Subject":"ABC","ID":"T001"},"1":{"yr":2018,"PKID":"1234,54321","Subject":"XYZ","ID":"T002"}}
print [{k: v} for k, v in d.items()]

Output:

输出:

[{'1': {'PKID': '1234,54321', 'yr': 2018, 'ID': 'T002', 'Subject': 'XYZ'}}, {'0': {'PKID': '58306, 57011', 'yr': 2017, 'ID': 'T001', 'Subject': 'ABC'}}]

回答by RoadRunner - MSFT

What about something like this:

像这样的事情怎么样:

from operator import itemgetter

d = {"0":{"yr":2017,"PKID":"58306, 57011","Subject":"ABC","ID":"T001"},"1":
    {"yr":2018,"PKID":"1234,54321","Subject":"XYZ","ID":"T002"}}

sorted_d = sorted(d.items(), key=lambda x: int(x[0]))

print(list(map(itemgetter(1), sorted_d)))

Which Outputs:

哪些输出:

[{'yr': 2017, 'PKID': '58306, 57011', 'Subject': 'ABC', 'ID': 'T001'}, 
 {'yr': 2018, 'PKID': '1234,54321', 'Subject': 'XYZ', 'ID': 'T002'}]