Pandas 在多列中提取字符串列

Pandas 在多列中提取字符串列,pandas,slice,extract,Pandas,Slice,Extract,我有下一个df: Match Count Asren_Blue_Coached 879 Asren_Blue_NotCoached 721 Asren_RED_Coached 6567 Asren_RED_NotCoached 764 Asren_White_Coached 762 Asren_White_NotCoached 431 Asren_Ye

我有下一个df:

Match                       Count
Asren_Blue_Coached           879
Asren_Blue_NotCoached        721
Asren_RED_Coached            6567
Asren_RED_NotCoached         764
Asren_White_Coached          762
Asren_White_NotCoached       431
Asren_Yellow_Coached         375
Asren_Yellow_NotCoached      412
Hrew_Blue_Coached            689
Hrew_Blue_NotCoached         634
...
我想添加三个新列

“匹配”列中下划线前的每个单词对应一列

因此,预期输出如下所示:

Match                       NewCol1   NewCol2    NewCol3      Count    
Asren_Blue_Coached           Asren      Blue     Coached       879      
Asren_Blue_NotCoached        Asren      Blue     NotCoached    721
Asren_RED_Coached            Asren      RED      Coached       6567
Asren_RED_NotCoached         Asren      RED      NotCoached    764
Asren_White_Coached          Asren      White    Coached       762
Asren_White_NotCoached       Asren      White    NotCoached    431
Asren_Yellow_Coached         Asren      Yellow   Coached       375
Asren_Yellow_NotCoached      Asren      Yellow   NotCoached    412
Hrew_Blue_Coached            Hrew       Blue     Coached       689
Hrew_Blue_NotCoached         Hrew       Blue     NotCoached    634
...
你知道我怎么做吗

我正在Jupyter中使用pandas

您可以使用分隔符拆分列。然后只需标记
expand=True
即可获得新列,而不是列表

df["Match"].str.split("_", expand=True)

您可以使用
str.split
,将返回值指定给3列,并将
分隔符指定为“\

见下文:

df[['NewCol1', 'NewCol2','NewCol3']] = df['Match'].str.split('_', expand=True)
用于打印所需的输出:

print(df)
                     Match  Count NewCol1 NewCol2     NewCol3
0       Asren_Blue_Coached    879   Asren    Blue     Coached
1    Asren_Blue_NotCoached    721   Asren    Blue  NotCoached
2        Asren_RED_Coached   6567   Asren     RED     Coached
3     Asren_RED_NotCoached    764   Asren     RED  NotCoached
4      Asren_White_Coached    762   Asren   White     Coached
5   Asren_White_NotCoached    431   Asren   White  NotCoached
6     Asren_Yellow_Coached    375   Asren  Yellow     Coached
7  Asren_Yellow_NotCoached    412   Asren  Yellow  NotCoached
8        Hrew_Blue_Coached    689    Hrew    Blue     Coached
9     Hrew_Blue_NotCoached    634    Hrew    Blue  NotCoached