繁体   English   中英

嵌套字典到 pandas DataFrame -

[英]Nested dictionary to pandas DataFrame -

需要以下嵌套字典的帮助,我想将其转换为 pandas 数据框

结构类型:

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

实际数据 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 你需要的是json_normalize datapoints设置为record_path并将metricunit设置为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:

  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

我的 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': '最小值', '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': '最小值', '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': '最小值', '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' :'秒'}]

以及更多实例详细信息(接近 8K 实例信息)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM