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

时间:2017-10-16 07:54:03

标签: python json pandas

建议我在(某些嵌套的)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数据帧的长度,以产生以下数据? / p>

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

感谢。

1 个答案:

答案 0 :(得分: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