我有一个看起来像这样的JSON:
{
"4.0": {
"A1": {
"dR-14": 1.181,
"ev": 1.102,
"move11": 1.259,
"move6": 1.259,
"sILo": 1.259,
"tR-14": 1.04
},
"A2": {
"dR-03": 0.418,
"ev": -0.177,
"move11": 1.663,
"move6": 1.663,
"sILo": 0.418,
"tR-03": 0.818
},
"A3": {
"dR-16": 3.956,
"ev": 3.667,
"move11": 4.179,
"sILo": 4.246,
"tR-16": 3.465
},
...
我正试图将其放入看起来像这样的熊猫df
var1 var2 dR ev move11 move6 sILo tR
4.0 A1 1.181 1.102 1.259 1.259 1.259 1.04
4.0 A2 0.418 -0.177 1.663 1.663 0.418 0.818
4.0 A3 3.956 3.667 4.179 NaN 4.246 3.465
我尝试像这样使用pandas json_normalize:
js = pd.read_json('path', orient='index', typ='series', convert_dates=False, convert_axes = True)
pd.json_normalize(js, record_prefix = True)
但这会结合第一和第二个索引,因此我最终得到一个如下所示的df:
A1.0.2 A2.0.8 ...
0 1.0 1.0
1 NaN NaN
我为read_json和json_normalize尝试了几种不同的arg组合,所有结果都相似。
答案 0 :(得分:0)
使用:
# STEP 1
df = pd.DataFrame(data).stack()
# STEP 2
df = df.apply(pd.Series).rename_axis(['var1', 'var2']).reset_index()
# STEP 3
df['dR'] = df.filter(like='dR').stack().reset_index(drop=True)
df['tR'] = df.filter(like='tR').stack().reset_index(drop=True)
# STEP 4
m = df.columns.str.contains(r'^dR-\d+') | df.columns.str.contains(r'^tR-\d+')
df = df.loc[:, ~m]
步骤:
# STEP 1
A1 4.0 {'dR-14': 1.181, 'ev': 1.102, 'move11': 1.259,...
A2 4.0 {'dR-03': 0.418, 'ev': -0.177, 'move11': 1.663...
A3 4.0 {'dR-16': 3.956, 'ev': 3.667, 'move11': 4.179,...
# STEP 2
var1 var2 dR-14 ev move11 move6 sILo tR-14 dR-03 tR-03 dR-16 tR-16
0 4.0 A1 1.181 1.102 1.259 1.259 1.259 1.04 NaN NaN NaN NaN
1 4.0 A2 NaN -0.177 1.663 1.663 0.418 NaN 0.418 0.818 NaN NaN
2 4.0 A3 NaN 3.667 4.179 NaN 4.246 NaN NaN NaN 3.956 3.465
# STEP 3
var1 var2 dR-14 ev move11 move6 sILo tR-14 dR-03 tR-03 dR-16 tR-16 dR tR
0 4.0 A1 1.181 1.102 1.259 1.259 1.259 1.04 NaN NaN NaN NaN 1.181 1.040
1 4.0 A2 NaN -0.177 1.663 1.663 0.418 NaN 0.418 0.818 NaN NaN 0.418 0.818
2 4.0 A3 NaN 3.667 4.179 NaN 4.246 NaN NaN NaN 3.956 3.465 3.956 3.465
# STEP 4 (RESULT)
var1 var2 ev move11 move6 sILo dR tR
0 4.0 A1 1.102 1.259 1.259 1.259 1.181 1.040
1 4.0 A2 -0.177 1.663 1.663 0.418 0.418 0.818
2 4.0 A3 3.667 4.179 NaN 4.246 3.956 3.465