0

I have this dataset:

pd.DataFrame(data={'ID':['1','2','3'],'Genre':['Adventure|Children|Fantasy','Horror','Comedy|Drama']})

    ID  Genre
0   1   Adventure|Children|Fantasy
1   2   Horror
2   3   Comedy|Drama

I want it to look like this:

    ID  Genre
0   1   Adventure
1   1   Children
2   1   Fantasy
3   2   Horror
4   3   Comedy
5   3   Drama

How can I do it with Pandas?

beridzeg45
  • 246
  • 2
  • 11

2 Answers2

1

With df.set_index + .explode() on splitted Genre:

df.set_index('ID')['Genre'].str.split('|').explode().reset_index()

  ID      Genre
0  1  Adventure
1  1   Children
2  1    Fantasy
3  2     Horror
4  3     Comedy
5  3      Drama
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

I would simply extractall the words and join them with the corresponding IDs :

out = df[["ID"]].join(df["Genre"].str.extractall(r"(?P<Genre>[^|]+)").droplevel(1))

​ Output :

print(out)

  ID      Genre
0  1  Adventure
0  1   Children
0  1    Fantasy
1  2     Horror
2  3     Comedy
2  3      Drama
Timeless
  • 22,580
  • 4
  • 12
  • 30