简体   繁体   中英

Split and explode a string with different amount of arrays of a pandas dataframe to separate rows

I try to split a column of text strings which contains comma-separated values (Genres by Song). For further analysis I need every value of the string in a new row. I am new to python. Managed to import the excel in a pandas dataframe. Couldn't find an example of my problem on stack.

df:

Song Title Genres Length
Song1 HipHop, Rap 3:30
Song2 Rock, Metal,Hard Rock 2:55
Song3 Jazz 4:00

What I try to achieve:

Song Title Genres Length
Song1 HipHop 3:30
Song1 Rap 3:30
Song2 Rock 2:55
Song2 Metal 2:55
Song2 Hard Rock 2:55
Song3 Jazz 4:00

Perfect job for explode :

df["Genres"] = df["Genres"].apply(lambda g: g.split(","))
df = df.explode("Genres")

# To avoid surprise down the road, remove
# leading and trailing spaces with strip
df["Genres"] = df["Genres"].str.strip()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM