简体   繁体   English

如何将字符串拆分为 python pandas 中的不同列

[英]How to split a string into different columns in python pandas

I have looked at multiple stack overflow questions similar to this, for example, this one and this one .我已经查看了多个与此类似的堆栈溢出问题,例如this onethis one However, my situation is a bit more tedious.但是,我的情况有点乏味。

I have column consisting of the following strings我有由以下字符串组成的列

 lineup
0 'FLEX Nick Mullens FLEX Raheem Mostert FLEX Deebo Samuel FLEX Cole Beasley FLEX Jordan Reed CPT Robbie Gould'
1 'FLEX Nick Mullens FLEX Brandon Aiyuk FLEX Cole Beasley FLEX Tyler Bass FLEX Robbie Gould CPT Raheem Mostert'
2 'FLEX Josh Allen FLEX Stefon Diggs FLEX Cole Beasley FLEX Devin Singletary FLEX Richie James CPT Bills'
...

I would like to transform that string into multiple rows, like so:我想将该字符串转换为多行,如下所示:

     FLEX1           FLEX2         FLEX3          FLEX4           FLEX5         CPT
0 Nick Mullens  Raheem Mostert  Deebo Samuel  Cole Beasley     Jordan Reed   Robbie Gould
1 Nick Mullens  Brandon Aiyuk   Cole Beasley  Tyler Bass       Robbie Gould  Raheem Mostert
2 Josh Allen    Stefon Diggs    Cole Beasley  Devin Singletary Richie James  Bills

So, I would like to expand the FLEX and CPT into separate columns, and the following name should be placed in the according column.因此,我想将 FLEX 和 CPT 扩展为单独的列,并将以下名称放在相应的列中。

I know how to use str.split() , but I'm not sure how to turn part of the string into new columns.我知道如何使用str.split() ,但我不确定如何将部分字符串转换为新列。 Any help is appreciated, thanks!任何帮助表示赞赏,谢谢!

Use str.slit with expand :str.slitexpand一起使用:

df.lineup.str.split('FLEX|CPT',expand=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM