0

I have a dataframe that looks like this:

Movie        Action    Adventure    Duration Director   More info...
SpongeBob       0          1           123      Karl
The Avengers    1          1           134      Annie

But I want to split that up into lines containing only the movie, one genre at a time and some more columns, like 'Director' and 'Duration'. After the transformation, the DataFrame should look like this:

Movie            Genre       Duration Director  (No more extra info)
SpongeBob      Adventure        123      Karl
The Avengers   Adventure        134      Annie
The Avengers   Action           134      Annie
Joao Donasolo
  • 359
  • 1
  • 9

1 Answers1

1

Here is one way to do it:

df.melt(['Movie', 'Duration', 'Director', 'More info'], var_name='Genre')\
  .query('value == 1')\
  .drop('value', axis=1)

Output:

          Movie  Duration Director More info      Genre
1  The Avengers       134    Annie         B     Action
2     SpongeBob       123     Karl         A  Adventure
3  The Avengers       134    Annie         B  Adventure

Reshape the dataframe using melt then filter for rows where value equals to 1, then drop the value column.

Scott Boston
  • 147,308
  • 15
  • 139
  • 187