I have a columns in a dataframe like this:
column_name
{"trials": {"value": ["8"]}, "results": {"value": "malfuction details."}, "trials_nic": {"value": ["7"]}, "custom_cii": {"value": ["yes"]}}
When I apply type to this column I get "str"
df['column_name'].apply(type)
output: <class 'str'>
How can I flat this column to get each key:value
pair in a new column?
CodePudding user response:
The canonical way to explode a column of dictionaries is:
new_df = pd.DataFrame(df['column_name'].tolist())
However, you probably want the additional step of extracting your values from the dictionaries they are contained within:
new_df = new_df.applymap(lambda d: d['value'])
Combining both steps into a single one-liner:
new_df = pd.DataFrame(df['column_name'].tolist()).applymap(lambda d: d['value'])
Which gives you what I assume is your desired output:
CodePudding user response:
you can use:
dictt = {"trials": {"value": ["8"]}, "results": {"value": "malfuction details."}, "trials_nic": {"value": ["7"]}, "custom_cii": {"value": ["yes"]}}
df=pd.DataFrame(data={'column_name':[dictt]})
print(df)
'''
column_name
0 {'trials': {'value': ['8']}, 'results': {'value': 'malfuction details.'}, 'trials_nic': {'value': ['7']}, 'custom_cii': {'value': ['yes']}}
'''
df=df['column_name'].apply(pd.Series).T
df=df[0].apply(pd.Series).explode('value').T
print(df)
'''
trials results trials_nic custom_cii
value 8 malfuction details. 7 yes
'''