Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Splitting a column into multiple using regular expression

I have the following table

df = pd.DataFrame({'favs':{0:'chicken_panfry1_t360_ketchup',
                          1:'chicken_bake2_t450_out_bbq',
                          2:'chicken_boiled2_season_gravy'}})

That looks like this

   favs
0 chicken_panfry1_t360_ketchup
1 chicken_bake2_t450_out_bbq
2 chicken_boiled2_season_gravy

I would like to split the column at the last underscore to create 2 new columns that look like this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 favs                              recipe                     sauce
0 chicken_panfry1_t360_ketchup     chicken_panfry1_t360       ketchup
1 chicken_bake2_t450_out_bbq       chicken_bake2_t450_out     bbq
2 chicken_boiled2_season_gravy     chicken_boiled2_season     gravy

This is what I’ve tried

df[['recipe','sauce']]=df['favs'].str.split(r'.*_', expand=True)

This creates the sauce column correctly but the recipe column is blank. It looks like this. Unsure of how to correct it.

 favs                              recipe       sauce
0 chicken_panfry1_t360_ketchup                  ketchup
1 chicken_bake2_t450_out_bbq                    bbq
2 chicken_boiled2_season_gravy                  gravy

>Solution :

You need Series.str.extract with the (.*)_(.*) regex pattern:

df[['recipe','sauce']]=df['favs'].str.extract(r'(.*)_(.*)', expand=True)

See the regex demo.

The (.*)_(.*) regex matches and captures the part before the last _ into Group 1 (with the first (.*)) and the part after last _ into the second column (with the second (.*)).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading