Unable to access dictionary values stored in pandas dataframe

Question:

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.

Asked By: amnesic

||

Answers:

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.

Answered By: Damian SowiƄski
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.