Skip to content
Advertisement

How to access data and handle missing data in a dictionaries within a dataframe

Given, df:

JavaScript

Input Dataframe:

JavaScript

My expected output dataframe is df[['Col1', 'Income', 'Age', 'Street', 'Zip']] where Income, Age, Street, and Zip come from within Person:

JavaScript

Advertisement

Answer

Using list comprehension, we can create most of these columns.

JavaScript

Output:

JavaScript

However, dealing with np.nan values inside a nested dictionary is a real pain. Let’s look at getting data from a nested dictionary data where one of the values is nan.

JavaScript

We get an AttributeError:

JavaScript

Let’s use the .str accessor with dictionary keys to fetch this data.
There is little documentation in pandas that shows how you can use .str.get or .str[] to fetch values from dictionary objects in a dataframe column/pandas series.

JavaScript

Output:

JavaScript

And, likewise with

JavaScript

Leaving us with the columns to build the desired dataframe df[['Col1', 'Income', 'Age', 'Street', 'Zip']] from dictionaries.

Output:

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