Skip to content
Advertisement

Adding to a nested Dictionary in a Column in Pandas

So, I have a fun issue. I have some data that have a fun nested dictionary that I need to manipulate, but am having trouble. I can do it in pure python, but wanted to do the entire solution in Pandas so as to keep the code a little cleaner and not have to re-open the same files elsewhere.

Dataframe:

JavaScript

Desired output, a list of dictionaries, placing the Id and Timezone into the nested dictionary while wrapping each in another key so I can just output as JSON:

JavaScript

The problem is that I need to, for the final data format to be ingested elsewhere, place Timezone and Id into the nested portion of each row to be exported into JSON and rename some fields. I’ve tried the iterrows method and apply with an axis of 1 but it ends up putting all Id’s and timezones in each dictionary, though it does nest them.

A variation of below works in pure python when reading in the whole CSV, but not in Pandas (for reasons that are likely obvious for most). For brevity, these are the rows I’m interested in, so I drop any others.

JavaScript

Any help would be appreciated.

Advertisement

Answer

One way:

  1. Create another list of dict via to_dict('records').
  2. zip and iterate over both the list of dict.
  3. Update the 1st one with the other to get the desired JSON.
JavaScript

OUTPUT:

JavaScript
Complete Example:
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement