【发布时间】:2020-07-27 21:03:42
【问题描述】:
我在event_records 中有一个字典列表,下面是该列表的一个子集。每个字典包含 2 或 3 个键值对。第一个键是item,对应的值是event#status。
第二个键是count,对应的值由一个包含 8 个键值对 + 1 个键值对的字典组成,其中值是 9 个字典的列表,每个字典包含 3 个键值对。
第三个键(仅在某些时候出现)是errors,对应的值是一个字典,列表中有 3 个键值对。
将event_records 中的以下字典列表转换为熊猫数据框的最有效方法是什么?我试过下面的代码,但是速度和性能都很慢。
from pandas.io.json import json_normalize
import pandas as pd
df1 = json_normalize(event_records)
df2 = df1['customEvents']
custom_events_list = []
for element in df2:
df3 = json_normalize(element)
df4 = df3[['type', 'value']]
df5 = df4.T
df5.columns = df5.iloc[0]
df5 = df5[1:]
custom_events_list.append(df5)
df6 = pd.concat(custom_events_list)
df6 = df6.reset_index(drop = True)
df7 = df1.join(df6)
df8 = df1['errors']
event_error_list = []
for element in df8:
df9 = json_normalize(element)
df10 = df9[['response', 'feedback']]
event_error_list.append(df10)
df11 = pd.concat(event_error_list)
df11 = df11.reset_index(drop = True)
df12 = df7.join(df11)
df13 = df12[['old_id', 'new_id', 'event_id', 'event_time', 'value', 'quantity', 'unique_id', 'A3', 'A4', 'A6', 'A9', 'A10', 'A11', 'A12', 'A13', 'A14', 'response', 'feedback']]
event_records = [{'item': 'event#status',
'count': {'item': 'event#count',
'old_id': '123',
'new_id': '456',
'event_id': '111',
'event_time': '1200',
'value': 1.0,
'quantity': '1',
'unique_id': '222',
'customEvents': [{'item': 'event#custom', 'type': 'A3', 'value': ''},
{'item': 'event#custom', 'type': 'A4', 'value': '11AA'},
{'item': 'event#custom', 'type': 'A6', 'value': 'AAB1'},
{'item': 'event#custom', 'type': 'A9', 'value': ''},
{'item': 'event#custom', 'type': 'A10', 'value': '10.5'},
{'item': 'event#custom', 'type': 'A11', 'value': 'ABC'},
{'item': 'event#custom', 'type': 'A12', 'value': 'NYR'},
{'item': 'event#custom', 'type': 'A13', 'value': 'NYR'},
{'item': 'event#custom', 'type': 'A14', 'value': 'NYR'}]},
'errors': [{'item': 'event#Error',
'response': 'NONE',
'feedback': 'Event not found'}]},
{'item': 'event#status',
'count': {'item': 'event#count',
'old_id': '567',
'new_id': '789',
'event_id': '333',
'event_time': '1400',
'value': 1.0,
'quantity': '1',
'unique_id': '444',
'customEvents': [{'item': 'event#custom', 'type': 'A3', 'value': ''},
{'item': 'event#custom', 'type': 'A4', 'value': '22BB'},
{'item': 'event#custom', 'type': 'A6', 'value': 'CCD1'},
{'item': 'event#custom', 'type': 'A9', 'value': ''},
{'item': 'event#custom', 'type': 'A10', 'value': '20.5'},
{'item': 'event#custom', 'type': 'A11', 'value': 'ABC'},
{'item': 'event#custom', 'type': 'A12', 'value': 'NYR'},
{'item': 'event#custom', 'type': 'A13', 'value': 'NYR'},
{'item': 'event#custom', 'type': 'A14', 'value': 'NYR'}]}}]
所需的 Pandas 数据帧输出如下:
old_id new_id event_id event_time value quantity unique_id A3 A4 A6 A9 A10 A11 A12 A13 A14 response feedback
123 456 111 1200 1.0 1 222 11AA AAB1 10.5 ABC NYR NYR NYR NONE Event not found
567 789 333 1400 1.0 1 444 22BB CCD1 20.5 ABC NYR NYR NYR
【问题讨论】:
-
请检查您的 event_records 数据,它似乎没有正确格式化
-
谢谢。我更新了 event_records,现在格式正确。
标签: python python-3.x pandas dataframe dictionary