0

I have data in a Pandas dataframe in the following format:

Campaign | Spend |

L003-FL-Panama | 800 |

L015, L020 CA- Rancho | 600 |

What I need is to append each value from the Campaign column that starts with a capital 'L' and ends with three digits to a new column. To make matters more complicated, if there are multiple values in this format within a string in the campaign column, then I need each value to be listed in its own new row.

The output would be the following:

Campaign | Spend | Store

L003-FL-Panama | 800 | L003

L015, L020 CA- Rancho | 600 | L015

L015, L020 CA- Rancho | 600 | L020

Apologies if this doesn't make sense, let me know if I can clarify.

1
  • Can you show us the code for your latest attempts, including it in your question? Commented Aug 29, 2018 at 17:35

1 Answer 1

2

You could use extractall then merge to the original dataframe

 df.reset_index().merge(df.Campaign.str.extractall('(\\bL\\d{3})').reset_index(),
           left_on='index',right_on  = 'level_0').drop(['index','level_0','match'],
           axis = 1).rename({0:'store'},axis = 1)

Out[65]: 
                 Campaign  Spend store
0         L003-FL-Panama     800  L003
1  L015, L020 CA- Rancho     600  L015
2  L015, L020 CA- Rancho     600  L020
Sign up to request clarification or add additional context in comments.

1 Comment

Yes! Thank you so much

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.