Skip to content
Advertisement

New rows based on a string – Pandas. python

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?

Advertisement

Answer

You can use spit and explode:

df['cast'] = df['cast'].str.split(',')
df.explode('cast')
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement