基于字符串 - 熊猫的新行。 Python

发布于 2025-02-03 03:57:57 字数 748 浏览 4 评论 0原文

我有这个熊猫DF,

show_id     type           title            director        cast    

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

我需要能够以几行分解“铸造”字段 示例:

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笙痞 2025-02-10 03:57:57

您可以使用吐痰并爆炸:

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

You can use spit and explode:

df['cast'] = df['cast'].str.split(',')
df.explode('cast')
难忘№最初的完美 2025-02-10 03:57:57

您可以使用:

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

You can use:

out = (df.drop(columns='cast')
         .join(df['cast'].str.split(',')
                 .explode()
       )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文