vasili111
vasili111

Reputation: 6930

Filtering data frame rows with regex

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?

Upvotes: 0

Views: 31

Answers (1)

BENY
BENY

Reputation: 323226

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

Upvotes: 2

Related Questions