How to access a dict column field in pandas

Viewed 1170

Having the follow dataframe

df = pd.DataFrame([[30, 20, {'some_data': 30}]], columns=['a', 'b', 'c'])

I would like to create a new column with the value of some_data value. I was thinking something like:

df['new_column'] = df['c']['some_data']

Is there a simple way to do it? In reality the dict would be more complex, I will have to get a nested value.

EDIT 1:

Here is a example where I have nested data, it's closer to real problem.

df = pd.DataFrame([[30, 20, {'some_data': [{'other_data': 0}]}]], columns=['a', 'b', 'c'])
# I would like to do something like:
df['new_column'] = df['c']['some_data'][0]['other_data'] 
3 Answers

Use the .str accessor:

df.c.str['some_data']

#0    30
#Name: c, dtype: int64

You can further chain .str for nested data access, given:

df = pd.DataFrame([[30, 20, {'some_data': [{'other_data': 0}]}]], columns=['a', 'b', 'c'])
df
#    a   b                                   c
#0  30  20  {'some_data': [{'other_data': 0}]}

To access nested other_data field, you can do:

df.c.str['some_data'].str[0].str['other_data']
#0    0
#Name: c, dtype: int64

Perhaps something like this: y = [df['c'][df['c'].keys()[0]].values()]

Your example of what you are trying to do is rather close to what should be done. If I understand your question correctly, this snippet should work.

df['new_column'] = df['c'][0]['some_data'][0]['other_data']

If this is not the desired behaviour, then you can try this.

Related