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

Dataframe take rows that have exactly one occurence of a value from list

Code:

import pandas as pd 

df = pd.DataFrame({'data': ['hey how r u', 'hello', 'hey abc d e f hey f', 'g h i i j k', 'hello how r u hello']})
vals = ['hey', 'hello']

I want to take all the rows that have exactly one word that is in the list vals. In this case, these would be 'hey how r u', 'hello'

What I tried:

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

def exactly_one(text):
    for v in vals:
        if text.count(v) > 1:
            return False
    return True


df = df[df['data'].contains('|'.join(vals)) & (exactly_one(df['data'].str))]

Breaks with an error

>Solution :

You can use str.count with a regex:

df[df['data'].str.count('|'.join(vals)).eq(1)]

Output:

          data
0  hey how r u
1        hello

Intermediate:

df['data'].str.count('|'.join(vals))

0    1
1    1
2    2
3    0
4    2
Name: data, dtype: int64
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