[英]Nested dictionary to pandas DataFrame -
Need help on the below nested dictionary and I want to convert this to a pandas Data Frame需要以下嵌套字典的帮助,我想将其转换为 pandas 数据框
Structure type:结构类型:
DS = [{ 'Outer_key1.0' : [{ 'key1.0': 'data' , 'key2.0': 'data' , 'key3.0': 'data } ,
{ 'key1.1': 'data' , 'key2.1': 'data' , 'key3.1': 'data } ,
{ 'key1.2': 'data' , 'key2.2': 'data' , 'key3.3': 'data } ,]
'Outer key2.0': 'data' ,
'Outer Key3.0': 'data' }]
[{ 'Outer_key1.1' : [{ 'key1.0': 'data' , 'key2.0': 'data' , 'key3.0': 'data } ,
{ 'key1.1': 'data' , 'key2.1': 'data' , 'key3.1': 'data } ,
{ 'key1.2': 'data' , 'key2.2': 'data' , 'key3.3': 'data } ,]
'Outer key2.1': 'data' ,
'Outer Key3.1': 'data' }]
Actual data model as mentioned below实际数据 model 如下所述
[{'datapoints': [{'statistic': 'Minimum', 'timestamp': '2021-08-31 06:50:00.000000', 'value': 59.03},{'statistic': 'Minimum', 'timestamp': '2021-08-18 02:50:00.000000', 'value': 59.37}, {'statistic': 'Minimum', 'timestamp': '2021-08-24 16:50:00.000000', 'value': 58.84},...],'metric': 'VolumeIdleTime', 'unit': 'Seconds'}]
cc= pd.Series(DS).apply(lambda x : pd.Series({ k: v for y in x for k, v in y.items() }))
IIUC what you need is json_normalize
. IIUC 你需要的是
json_normalize
。 Set datapoints
as record_path
and metric
and unit
as meta
:将
datapoints
设置为record_path
并将metric
和unit
设置为meta
:
data = [{'datapoints': [{'statistic': 'Minimum', 'timestamp': '2021-08-31 06:50:00.000000', 'value': 59.03},{'statistic': 'Minimum', 'timestamp': '2021-08-18 02:50:00.000000', 'value': 59.37}, {'statistic': 'Minimum', 'timestamp': '2021-08-24 16:50:00.000000', 'value': 58.84}],'metric': 'VolumeIdleTime', 'unit': 'Seconds'}]
df = pd.json_normalize(data, record_path="datapoints", meta=["metric", "unit"])
print(df)
Output: Output:
statistic timestamp value metric unit
0 Minimum 2021-08-31 06:50:00.000000 59.03 VolumeIdleTime Seconds
1 Minimum 2021-08-18 02:50:00.000000 59.37 VolumeIdleTime Seconds
2 Minimum 2021-08-24 16:50:00.000000 58.84 VolumeIdleTime Seconds
@Tranbi @Tranbi
My JSON have the following instance of CPU data and comes with random occurrence:我的 JSON 有以下 CPU 数据实例并且随机出现:
Instance1 [{'datapoints': [{'statistic': 'Minimum', 'timestamp': '2021-08-31 06:50:00.000000', 'value': 59.03}, {'statistic': 'Minimum', 'timestamp': '2021-08-18 02:50:00.000000', 'value': 59.37}, {'statistic': 'Minimum', 'timestamp': '2021-08-24 16:50:00.000000', 'value': 58.84},, 'metric': 'VolumeIdleTime', 'unit': 'Seconds'}] Instance1 [{'datapoints': [{'statistic': 'Minimum', 'timestamp': '2021-08-31 06:50:00.000000', 'value': 59.03}, {'statistic': 'Minimum', 'timestamp': '2021-08-18 02:50:00.000000', 'value': 59.37}, {'statistic': '最小值', 'timestamp': '2021-08-24 16:50:00.000000', 'value': 58.84},, 'metric': 'VolumeIdleTime', 'unit': 'Seconds'}]
Instance2 [{'datapoints': [{'statistic': 'Minimum', 'timestamp': '2021-08-31 06:50:00.000000', 'value': 60}, {'statistic': 'Minimum', 'timestamp': '2021-08-18 02:50:00.000000', 'value': 55.45}, {'statistic': 'Minimum', 'timestamp': '2021-08-24 16:50:00.000000', 'value': 54.16}, {'statistic': 'Minimum', 'timestamp': '2021-08-06 07:50:00.000000', 'value': 50.03}, {'statistic': 'Minimum', 'timestamp': '2021-08-04 22:50:00.000000', 'value': 60}, {'statistic': 'Minimum', 'timestamp': '2021-08-26 01:50:00.000000', 'value': 60.34}, 'metric': 'VolumeIdleTime', 'unit': 'Seconds'}] Instance2 [{'datapoints': [{'statistic': 'Minimum', 'timestamp': '2021-08-31 06:50:00.000000', 'value': 60}, {'statistic': 'Minimum', 'timestamp': '2021-08-18 02:50:00.000000', 'value': 55.45}, {'statistic': '最小值', 'timestamp': '2021-08-24 16:50:00.000000', 'value': 54.16}, {'statistic': '最小值', 'timestamp': '2021-08-06 07:50:00.000000', 'value': 50.03}, {'statistic': '最小值', '时间戳':'2021-08-04 22:50:00.000000','值':60},{'统计':'最小值','时间戳':'2021-08-26 01:50:00.000000','值':60.34},'metric':'VolumeIdleTime','unit':'Seconds'}]
Instance3 [{'datapoints': [{'statistic': 'Minimum', 'timestamp': '2021-08-31 06:50:00.000000', 'value': 60}, {'statistic': 'Minimum', 'timestamp': '2021-08-18 02:50:00.000000', 'value': 38.12}, {'statistic': 'Minimum', 'timestamp': '2021-08-24 16:50:00.000000', 'value': 42.31}, {'statistic': 'Minimum', 'timestamp': '2021-08-06 07:50:00.000000', 'value': 45.22}, {'statistic': 'Minimum', 'timestamp': '2021-08-04 22:50:00.000000', 'value': 40.51}, {'statistic': 'Minimum', 'timestamp': '2021-08-26 01:50:00.000000', 'value': 34.35}, {'statistic': 'Minimum', 'timestamp': '2021-08-11 12:50:00.000000', 'value': 46.33},'metric': 'VolumeIdleTime', 'unit': 'Seconds'}] Instance3 [{'datapoints': [{'statistic': 'Minimum', 'timestamp': '2021-08-31 06:50:00.000000', 'value': 60}, {'statistic': 'Minimum', 'timestamp': '2021-08-18 02:50:00.000000', 'value': 38.12}, {'statistic': 'Minimum', 'timestamp': '2021-08-24 16:50:00.000000', 'value': 42.31}, {'statistic': '最小值', 'timestamp': '2021-08-06 07:50:00.000000', 'value': 45.22}, {'statistic': '最小值', '时间戳':'2021-08-04 22:50:00.000000','值':40.51},{'统计':'最小值','时间戳':'2021-08-26 01:50:00.000000',' value': 34.35}, {'statistic': 'Minimum', 'timestamp': '2021-08-11 12:50:00.000000', 'value': 46.33},'metric': 'VolumeIdleTime', 'unit' :'秒'}]
And many more instance details to following ( Close to 8K instance information )以及更多实例详细信息(接近 8K 实例信息)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.