Home > OS >  Unable to access dictionary values stored in pandas dataframe
Unable to access dictionary values stored in pandas dataframe

Time:07-28

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.

CodePudding user response:

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.

  • Related