1

I have the next 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
...

I want to add three new columns,

One column for each word before the underscore in column 'Match'

So, the expected output looks like this:

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

Do you know how can I do it?

I'm working with pandas in Jupyter

2 Answers 2

2

You can use str.split, and assign the return to 3 columns named are you wish, and specify the delimiter to be "_"

See below:

df[['NewCol1', 'NewCol2','NewCol3']] = df['Match'].str.split('_', expand=True)

which prints your desired output:

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
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the Series.str.split() to split a column on a delimiter. Then just mark expand=True to get new columns instead of a list.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.