I have a DataFrame which contains a certain column with Dictionaries.
I want to add a new header in the DataFrame for each key found on each element in the column that contains dicts, each new value assigned to those new cells should correspond to None if that element doesn't contain that header key and the respective key value otherwise.
Here's the data for testing and visualizing what I'm saying:
Importing dependencies:
import pandas as pd
import numpy as np
Creating a dictionary that contains a inner dictionary list:
data = {'string_info': ['User1', 'User2', 'User3'],
'dict_info': [{'elm1': 'attr5', 'elm2': 'attr9', 'elm3': 'attr33'},
{'elm5': 'attr31', 'elm7': 'attr13'},
{'elm5': 'attr28', 'elm1': 'attr23', 'elm2': 'attr33','elm6': 'attr33'}],
'int_info': [4, 24, 31],}
Creating an appropriate initial DataFrame for testing:
df = pd.DataFrame.from_dict(data)
df
Manually stating what I want as output:
data2 = {'string_info': ['User1', 'User2', 'User3'],
'elm1': ['attr5',None,'attr23'],
'elm2': ['attr9',None,'attr33'],
'elm3': ['attr33',None,None],
'elm4': [None,None,None],
'elm5': [None,'attr31',None],
'elm6': [None,None,'attr33'],
'elm7': [None,None,'attr13'],
'int_info': [4, 24, 31]}
The desired output would be:
df2 = pd.DataFrame.from_dict(data2)
df2
Thanks!