pandas from_json方法的用法 [英] pandas from_json method usage

查看:123
本文介绍了 pandas from_json方法的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的JSON文件,

I have a JSON file like below,

{ 
"A":1,
"B":2,
"C": [
      {"x":1,"y":2,"z":3},
      {"x":2,"y":7,"z":77}
     ]
}

pandas.from_json返回带有列A,B和C的数据帧.但是,实际上,我正在寻找带有x,y和z的列.有办法吗?

pandas.from_json returns me data frame with column A,B and C. But, actually I am looking for columns with x,y and z. Is there a way to get it?

推荐答案

您可以使用

You can use json_normalize:

json = { 
"A":1,
"B":2,
"C": [{"x":1,"y":2,"z":3 },
      {"x":2,"y":7,"z":77}]
}


from pandas.io.json import json_normalize    
df = json_normalize(json, 'C')

print (df)

   x  y   z
0  1  2   3
1  2  7  77

如果需要所有列:

df = json_normalize(json, 'C', ['A','B'])

print (df)
   x  y   z  B  A
0  1  2   3  2  1
1  2  7  77  2  1

这篇关于 pandas from_json方法的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆