Skip to main content
 首页 » 编程设计

python之解压 JSON 并使用 pandas 中其他字段的当前数据进行扩展

2024年10月01日6xing901022

建议我在(一些嵌套的)JSON 的一个字段中有格式的数据:

Name       Identifier                Data 
Joe        54872                     [{"ref":{"type":4,"id":86669},"side":"Buy","ratio":1},{"ref":{"type":4,"id":80843},"side":"Sell","ratio":1}] 
Jill       84756                     [{"ref":{"type":4,"id":75236},"side":"Buy","ratio":1},{"ref":{"type":4,"id":75565},"side":"Sell","ratio":1}] 

有没有一种简单的方法,而不是将 json 解压缩到它自己的数据帧中,然后将它与 len(n) 的每一行的固定数据连接起来,其中 n 是每个 json 数据帧的长度,以生成以下数据?

Name      Identifier       ref_type      ref_id      side     ratio 
Joe       54872            4             86669       buy      1 
Joe       54872            4             80843       sell     1 
Jill      84756            4             75236       buy      1 
Jill      84756            4             75565       sell     1 

谢谢。

请您参考如下方法:

我认为最好的是使用 json_normalize :

from pandas.io.json import json_normalize 
import json 
 
with open('file.json') as data_file:     
    data = json.load(data_file) 
 
df = json_normalize(data) 

编辑:

如果不可能使用:

import ast 
from pandas.io.json import json_normalize 
 
#convert strings to lists and dicts 
df['Data'] = df['Data'].apply(ast.literal_eval) 
#parse Data column 
df1 = pd.concat([json_normalize(x) for x in df['Data'].values.tolist()], keys= df.index) 
#append to original 
df1 = df.drop('Data', 1).join(df1.reset_index(level=1, drop=True)).reset_index(drop=True) 
print (df1) 
   Name  Identifier  ratio  ref.id  ref.type  side 
0   Joe       54872      1   86669         4   Buy 
1   Joe       54872      1   80843         4  Sell 
2  Jill       84756      1   75236         4   Buy 
3  Jill       84756      1   75565         4  Sell