简体   繁体   中英

Unable to access dictionary values stored in pandas dataframe

I have read in a json file into my pandas dataframe which now looks like this:

        document_nr    doc_type      doc_details.Summary.ID doc_details.Summary.date  ....
209     202207220341       A                  None                07/22/2022    
210     202207220217       B                  None                07/27/2022   
211     202207220327       C                  None                07/29/2022
....

My issue is that I cannot access column values that are originally nested dictionaries. Example I can do print(df['document_nr']) without any problems but I cannot do print(df.doc_details.Summary.ID) as it gives me the error:

AttributeError: 'DataFrame' object has no attribute 'doc_details'

Likewise for print(df.doc_details.Summary.date) .

I have also tried below code but got the same error.

df['summary'] = df['doc_details'].str.get("Summary")

I have no idea why it's giving me this issue. My original sample Json file looks like this:

[
    {
        "document_nr": "202207220914",
        "doc_type": "A",
        "doc_details": {
               "Summary": {
                    "ID": null,
                    "date": "07/22/2022",
                     .....}
                       }
    }
]

Please advise.

as whole doc_details.Summary.ID is actual name of your column, you should access this data like this:

print(df["doc_details.Summary.ID"])

When you are using dot notation df.doc_details.Summary.ID it's at first looking for property doc_details in df object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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