Created
May 4, 2023 15:22
-
-
Save mwestwood/4f6871d126f3effeab53ade77ab88e3b to your computer and use it in GitHub Desktop.
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
import pandas as pd | |
df = pd.DataFrame({ | |
'fruits': [['apple', 'banana'], ['orange', 'pear'], ['grape', 'kiwi', 'peach']] | |
}) | |
# stack the dataframe | |
df = df['fruits'].apply(pd.Series).stack().reset_index(level=1, drop=True).to_frame('fruit') | |
df.index.name = 'id' | |
df = df.reset_index() | |
print(df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment