将嵌套的JSON展平为多行

时间:2019-07-25 17:08:24

标签: python json pandas dataframe

如何解析嵌套的JSON?下面是一个示例: 我们捕获了3个参数,分别是timeStamp和value。

  

输入JSON:

{     “ Sensor”:“ seda_01”,     “位置”: {         “城市”:“洛杉矶”,         “州”:“ CA”     },     “ rain_value”:[         [             “ 1564073521”,             “ 0.02”         ],         [             “ 1564073522”,             “ 0.01”         ],         [             “ 1564073523”,             “ 0.03”         ]     ],     “ sun_value”:[         [             “ 1564073521”,             “ 0.11”         ],         [             “ 1564073522”,             “ 0.10”         ],         [             “ 1564073523”,             “ 0.13”         ]     ],     “ wind_value”:[         [             “ 1564073521”,             “ 0.21”         ],         [             “ 1564073522”,             “ 0.21”         ],         [             “ 1564073523”,             “ 0.23”         ]     ] }

输出dataframe

| Sensor| Location_City | Location_Sate| Rain_value_TS | Rain_value | Sun_value_TS | Sun_value |
------------------------------------------------------- ----------------
|  seda_01 | Los Angeles | CA | 1564073521 | 0.02 | 1564073521 | 0.11 |
|  seda_01 | Los Angeles | CA | 1564073522 | 0.01 | 1564073522 | 0.10 |
  

Codelt的一次尝试如下

import pandas as pd
from flatten_json import flatten
jsonDict = {'Sensor':'seda_01', 'Location':{'City':'Los Angeles','State':'CA'}, 'rain_value':[ ['1564073521', '0.02'],['1564073522', '0.01'],['1564073523', '0.03'] ],'sun_value':[ ['1564073521', '0.11'],['1564073522', '0.10'],['1564073523', '0.13'] ] , 'wind_value':[ ['1564073521', '0.21'],['1564073522', '0.21'],['1564073523', '0.23'] ]}
dic_flattened = (flatten({k:v}) for k,v in jsonDict.items())
df = pd.DataFrame(dic_flattened)
df.head()
  

但是输出是用Lot NAN捕获的

enter image description here

1 个答案:

答案 0 :(得分:1)

我有commented链接到我尝试的代码,因为OP在使用它时没有给我以荣誉。我决定将其发布为答案。

我的尝试,这仍然不是答案,但会有所帮助。

import pandas as pd
from flatten_json import flatten

jsonDict = {'Sensor':'seda_01', 'Location':{'City':'Los Angeles','State':'CA'}, 'rain_value':[ ['1564073521', '0.02'],['1564073522', '0.01'],['1564073523', '0.03'] ],'sun_value':[ ['1564073521', '0.11'],['1564073522', '0.10'],['1564073523', '0.13'] ] , 'wind_value':[ ['1564073521', '0.21'],['1564073522', '0.21'],['1564073523', '0.23'] ]}

dic_flattened = (flatten({k:v}) for k,v in jsonDict.items())

df = pd.DataFrame(dic_flattened)

print(df)

df.to_csv('output.csv',index=False)

输出

Sensor,Location_City,Location_State,rain_value_0_0,rain_value_0_1,rain_value_1_0,rain_value_1_1,rain_value_2_0,rain_value_2_1,sun_value_0_0,sun_value_0_1,sun_value_1_0,sun_value_1_1,sun_value_2_0,sun_value_2_1,wind_value_0_0,wind_value_0_1,wind_value_1_0,wind_value_1_1,wind_value_2_0,wind_value_2_1
seda_01,,,,,,,,,,,,,,,,,,,,
,Los Angeles,CA,,,,,,,,,,,,,,,,,,
,,,1564073521,0.02,1564073522,0.01,1564073523,0.03,,,,,,,,,,,,
,,,,,,,,,1564073521,0.11,1564073522,0.10,1564073523,0.13,,,,,,
,,,,,,,,,,,,,,,1564073521,0.21,1564073522,0.21,1564073523,0.23

查看实际情况here