0

Here is my data frame:

import pandas as pd


data = {'Period':['Group 1 vs Group 2:Change at 3 mo', 'Group 1:Change at 3 mo', 'Group 1 vs Group 2:Change at 3 mo', 'Group 2:Change at 3 mo'], 'estimate':[20, 21, 19, 18]}

df = pd.DataFrame(data)

Now I need to get only rows that in variable Period do not contain anywhere Group 1 vs Group 2. I tried this code:

df = df.loc[df['Period'].str.contains(pat = '(?!Group 1 vs Group 2)', regex = True)].reset_index(drop=True)

But it does not filters rows and I am getting original df as a result. How to fix my code so I will get only rows that in variable Period do not contain anywhere Group 1 vs Group 2?

1
  • df[~df['Period'].str.contains(r'Group 1 vs Group 2')] Commented Jul 16, 2020 at 16:02

1 Answer 1

2

You can try str.match

df[~df.Period.str.match('Group 1 vs Group 2')]
Out[85]: 
                   Period  estimate
1  Group 1:Change at 3 mo        21
3  Group 2:Change at 3 mo        18
Sign up to request clarification or add additional context in comments.

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.