简体   繁体   English

从.csv文件使用Pandas访问JSON子属性

[英]Accessing JSON sub attributes using Pandas from .csv file

I have this nested JSON data set which I have converted to .csv using pandas : 我有这个嵌套的JSON数据集,已使用pandas转换为.csv

[{
        "attribute1": "One",
        "attribute2": "Two",
        "attribute3": [{
            "attribute4": "Four",
            "attribute5": "Five"
        }, {
            "attribute4": "Four",
            "attribute5": "Five"
        }]
    }]

df = pd.DataFrame(data, columns=["attribute1", "attribute2", "attribute3"])
df.to_csv('example.csv')

The data in the column attribute3 is still JSON. attribute3的数据仍然是JSON。 How can I access the values of subattributes of attribute3 ie attribute4 and attribute5 using indexing? 如何使用索引访问attribute3的子attribute3值,即attribute4attribute5 For instance something like this: data[0][2:0] for getting data at zeroth row, second column and its sub attribute zero. 例如这样的data[0][2:0]data[0][2:0]用于在第0行,第二列及其子属性0处获取数据。

I would appreciate some help regarding how to access nested values. 我希望获得有关如何访问嵌套值的帮助。 Should I flatten the one single column that contains nested values? 我应该展平包含嵌套值的单个列吗? How can I do that? 我怎样才能做到这一点?

It would be easier to parse your original JSON ( data ) using json_normalize() : 使用json_normalize()解析原始的JSON( data )会更容易:

In [5]: pd.io.json.json_normalize(data, ['attribute3'], ['attribute1','attribute2'])
Out[5]:
  attribute4 attribute5 attribute1 attribute2
0       Four       Five        One        Two
1       Four       Five        One        Two

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM