Skip to content

Instantly share code, notes, and snippets.

@mwestwood
Created May 4, 2023 15:22
Show Gist options
  • Save mwestwood/4f6871d126f3effeab53ade77ab88e3b to your computer and use it in GitHub Desktop.
Save mwestwood/4f6871d126f3effeab53ade77ab88e3b to your computer and use it in GitHub Desktop.
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