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

问题描述 投票:4回答:5

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

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

python json pandas dictionary dataframe
5个回答
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}]

3
投票

这是一种方式:

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

请注意,有序字典按文本字符串键排序,如提供的那样。您可能希望在通过d = {int(k): v for k, v in d.items()}进行任何处理之前先将这些转换为整数。


0
投票

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

尝试改为:[d],其中d是你的原始字典(未转换为json


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

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'}]
© www.soinside.com 2019 - 2024. All rights reserved.