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

时间:2018-02-28 10:55:12

标签: python json pandas dictionary dataframe

我有一个字典,它从数据框转换如下:

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

词典:

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

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

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"}}
']

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

如何实现这一目标?

预期产出:

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

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

5 个答案:

答案 0 :(得分:10)

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}]

答案 1 :(得分:3)

这是一种方式:

implementation 'com.firebaseui:firebase-ui-database:3.2.2'

请注意,有序字典按文本字符串键排序,如提供的那样。您可能希望在通过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}] 进行任何处理之前先将这些转换为整数。

答案 2 :(得分:0)

您正在将字典转换为json字符串。然后,将结果字符串包装为一个列表。所以,当然,结果是列表中的字符串。

请改为:[d]其中d是您的原始字典(未转换为json

答案 3 :(得分:0)

您可以使用列表理解

<强>实施例

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()]

<强>输出:

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

答案 4 :(得分:0)

这样的事情:

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)))

哪个输出:

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