Skip to content

Instantly share code, notes, and snippets.

@mwestwood
Created May 4, 2023 15:30
Show Gist options
  • Save mwestwood/66fc1a6cfbf3e1ab85371250a4468f9f to your computer and use it in GitHub Desktop.
Save mwestwood/66fc1a6cfbf3e1ab85371250a4468f9f to your computer and use it in GitHub Desktop.
import pandas as pd
from ast import literal_eval
df = pd.DataFrame({
'fruits': ["['apple', 'banana']", "['orange', 'pear']", "['grape', 'kiwi', 'peach']"]
})
# convert the string representation of lists to actual lists
df['fruits'] = df['fruits'].apply(literal_eval)
# expand the lists into rows
df = df.explode('fruits').reset_index(drop=True)
df.index.name = 'id'
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment