基于字符串的新行 - Pandas。 python

New rows based on a string - Pandas. python

我有这个 pandas df

show_id     type           title            director        cast    

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

我需要能够将 'cast'' 字段分解为多行 示例:

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

我知道我应该用pandas来做,但是很复杂,你能帮我吗?

你可以使用 spit and explode:

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

您可以使用:

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