Created
May 4, 2023 15:30
-
-
Save mwestwood/66fc1a6cfbf3e1ab85371250a4468f9f 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 | |
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