Skip to content

Instantly share code, notes, and snippets.

@danielecook
Last active December 11, 2019 23:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielecook/06eb6fa31ef91030cb3c521ded7e8470 to your computer and use it in GitHub Desktop.
Save danielecook/06eb6fa31ef91030cb3c521ded7e8470 to your computer and use it in GitHub Desktop.
unnest pandas df
# https://benjcunningham.org/blog/unnest-list-columns-in-pandas.html
import pandas as pd
def unnest(df, col):
unnested = (df.apply(lambda x: pd.Series(x[col]), axis=1)
.stack()
.reset_index(level=1, drop=True))
unnested.name = col
return df.drop(col, axis=1).join(unnested)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment