Home > Mobile >  New rows based on a string - Pandas. python
New rows based on a string - Pandas. python

Time:05-31

I have this pandas df

show_id     type           title            director        cast    

  s1        Movie      Duck the Halls      Dave Wasson    Chris Diamantopoulos, Tony Anselmo, 
                                                           Tress MacNeille, Bill Farmer, 

I need to be able to break down the 'cast'' field in such a way that it is in several rows Example:

show_id     type           title            director        cast    

  s1        Movie      Duck the Halls      Dave Wasson    Chris Diamantopoulos
  s1        Movie      Duck the Halls      Dave Wasson    Tony Anselmo
  s1        Movie      Duck the Halls      Dave Wasson    Tress MacNeille
  s1        Movie      Duck the Halls      Dave Wasson    Bill Farmer

I understand that I should do it with pandas, but it is very complicated, can you help me?

CodePudding user response:

You can use spit and explode:

df['cast'] = df['cast'].str.split(',')
df.explode('cast')

CodePudding user response:

You can use:

out = (df.drop(columns='cast')
         .join(df['cast'].str.split(',')
                 .explode()
       )
  • Related