0

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?

2 Answers 2

1

You can use spit and explode:

df['cast'] = df['cast'].str.split(',')
df.explode('cast')
Sign up to request clarification or add additional context in comments.

Comments

0

You can use:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.