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

Check if a column contains only one particular value and nothing else

I have a dataframe like the following but much larger:

import pandas as pd
df = pd.DataFrame({'text': ['Can you open the door?', 'Open the window', 'Call the hair salon.'],'info': ['on', 'on', 'off']})

I would like to print different values depending on what is in the info column. for example:

if all values in 'info' == 'on':
   print('all values are on')
elif all values in 'info' == 'off':
    print('all values are off')
elif 'info' contains both ('on' and 'off'):
    print('both values exist')

so i did the following:

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

if all(df['info'].unique() == ['on']):
    print('all values are on')
elif all(df['info'].unique() == ['off']):
    print('all values are off')
elif all(df['info'].unique() == ['off','on']):
    print('both values exist')

But I do not get any output, and i need a solution that is consistent, so if unlike the example i gave all values are really just only ‘on’ or ‘off’, i would still get the right output.

>Solution :

Use sets for compare values, advantege is order is not important, same set is set(['off','on']) == set(['on','off']):

s = set(df['info'])

if s == set(['on']):
    print('all values are on')
elif (s == set(['off'])):
    print('all values are off')
elif (s == set(['off','on'])):
    print('both values exist')

General solution:

s = set(df['info'])

if len(s) == 1:
    print(f'all values are {list(s)[0]}')
else:
    print(f'multiple values exist - {", ".join(s)}')
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