TL;DR
df = df.drop('colC', axis=1).join(pd.DataFrame(df.colC.values.tolist()))
Elaborate answer
We start by defining the DataFrame to work with, as well as a importing Pandas:
import pandas as pd
df = pd.DataFrame({'colA': {0: 7, 1: 2, 2: 5, 3: 3, 4: 5},
'colB': {0: 7, 1: 8, 2: 10, 3: 2, 4: 5},
'colC': {0: {'foo': 185, 'bar': 182, 'baz': 148},
1: {'foo': 117, 'bar': 103, 'baz': 155},
2: {'foo': 165, 'bar': 184, 'baz': 170},
3: {'foo': 121, 'bar': 151, 'baz': 187},
4: {'foo': 137, 'bar': 199, 'baz': 108}}})
The column colC
is a pd.Series
of dicts, and we can turn it into a pd.DataFrame
by turning each dict into a pd.Series
:
pd.DataFrame(df.colC.values.tolist())
# df.colC.apply(pd.Series). # this also works, but it is slow
which gives the pd.DataFrame
:
foo bar baz
0 154 190 171
1 152 130 164
2 165 125 109
3 153 128 174
4 135 157 188
So all we need to do is:
- Turn
colC
into a pd.DataFrame
- Delete the original
colC
from df
- Join the convert
colC
with df
That can be done in a one-liner:
df = df.drop('colC', axis=1).join(pd.DataFrame(df.colC.values.tolist()))
With the contents of df
now being the pd.DataFrame
:
colA colB foo bar baz
0 2 4 154 190 171
1 4 10 152 130 164
2 4 10 165 125 109
3 3 8 153 128 174
4 10 9 135 157 188