Efficient and elegant:
tf = pd.DataFrame([
{'id': 1, 'nested': {'a': 1, 'b': 2} },
{'id': 2, 'nested': {'a': 2, 'b': 4} },
{'id': 3, 'nested': {'a': 3, 'b': 6} },
{'id': 4, 'nested': {'a': 4}},
])
def unpack(df, column, fillna=None):
ret = None
if fillna is None:
ret = pd.concat([df, pd.DataFrame((d for idx, d in df[column].iteritems()))], axis=1)
del ret[column]
else:
ret = pd.concat([df, pd.DataFrame((d for idx, d in df[column].iteritems())).fillna(fillna)], axis=1)
del ret[column]
return ret
unpack(tf, 'nested', 0)
will yield
id a b
0 1 1 2
1 2 2 4
2 3 3 6
3 4 4 0
If you want to create a projection (select subset of the keys in the nested dict) you can use `apply` before unpack or a column projection on the dataframe created inside unpack.