Last active
December 11, 2019 23:43
-
-
Save danielecook/06eb6fa31ef91030cb3c521ded7e8470 to your computer and use it in GitHub Desktop.
unnest pandas df
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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