如何解决如何将有序的dict转换为每列的每个键值“标签”的pandas数据帧
如何将有序的 dict 转换为 Pandas 数据框,每列的每个键(“标签”)。
```OderedDict = [OrderedDict([('dataCells',[OrderedDict([('label','BT Ltd'),('value','00120000000I3eZAAS')]),OrderedDict([('label','BP | Alert generated for Rejected FileAct message in GTE'),'5007R00002otgJRQAY')]),'12/27/2020 1:57 AM'),'2020-12-27T06:57:16Z')]),'52'),52)]),'false'),False)]),'true'),True)]),'00120000000I3eZ'),'GTE'),'GTE')]),'FM'),'FM')]),'Cust'),'Cust')]),'Incident'),'Incident')]),'07370371'),'5007R00002otgJR'),'BT UK'),'a93w000000001NSAAY')]),'Amanda Pandas'),'0052X000008LWapQAG')])])]),```
转换为熊猫数据框
df = pd.DataFrame(report_results['factMap']['T!T']['rows'] )
#use pd.Series 在数据框中创建列
df = df.dataCells.apply(pd.Series)
我已经尝试了上述操作,但最终得到了一系列有序的字典 ({'label': 'GTE','value': 'GTE'}) 我如何才能获得标签值“GTE”?
解决方法
创建数据框,展开 list
,然后将 dict
转换为列
from collections import OrderedDict
data = [OrderedDict([('dataCells',[OrderedDict([('label','BT Ltd'),('value','00120000000I3eZAAS')]),OrderedDict([('label','BP | Alert generated for Rejected FileAct message in GTE'),'5007R00002otgJRQAY')]),'12/27/2020 1:57 AM'),'2020-12-27T06:57:16Z')]),'52'),52)]),'false'),False)]),'true'),True)]),'00120000000I3eZ'),'GTE'),'GTE')]),'FM'),'FM')]),'Cust'),'Cust')]),'Incident'),'Incident')]),'07370371'),'5007R00002otgJR'),'BT UK'),'a93w000000001NSAAY')]),'Amanda Pandas'),'0052X000008LWapQAG')])])])]
# create DF,expand list,convert dict to columns
df = pd.DataFrame(data).explode("dataCells")["dataCells"].apply(lambda x: pd.Series(x))
输出
label value
BT Ltd 00120000000I3eZAAS
BP | Alert generated for Rejected FileAct message in GTE 5007R00002otgJRQAY
12/27/2020 1:57 AM 2020-12-27T06:57:16Z
52 52
false False
true True
00120000000I3eZ 00120000000I3eZAAS
GTE GTE
FM FM
Cust Cust
Incident Incident
07370371 5007R00002otgJRQAY
5007R00002otgJR 5007R00002otgJRQAY
00120000000I3eZ 00120000000I3eZAAS
BT UK a93w000000001NSAAY
Amanda Pandas 0052X000008LWapQAG
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。