1

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?

1
  • df.explode(['trials','trials_nic','custom_cii'])? Commented Oct 27, 2022 at 19:53

2 Answers 2

1

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:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

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

'''

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.