I have this pandas df
JavaScript
x
5
1
show_id type title director cast
2
3
s1 Movie Duck the Halls Dave Wasson Chris Diamantopoulos, Tony Anselmo,
4
Tress MacNeille, Bill Farmer,
5
I need to be able to break down the ‘cast” field in such a way that it is in several rows Example:
JavaScript
1
7
1
show_id type title director cast
2
3
s1 Movie Duck the Halls Dave Wasson Chris Diamantopoulos
4
s1 Movie Duck the Halls Dave Wasson Tony Anselmo
5
s1 Movie Duck the Halls Dave Wasson Tress MacNeille
6
s1 Movie Duck the Halls Dave Wasson Bill Farmer
7
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:
JavaScript
1
3
1
df['cast'] = df['cast'].str.split(',')
2
df.explode('cast')
3