簡體   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