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

Time:05-10

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:

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.

CodePudding user response:

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)}')

CodePudding user response:

To check if all values are "on" you can use:

df['info'].eq('on').all()
# False

You can also use:

out = df['info'].unique()

if len(out)>1:
    print('both values exist')
else:
    print(f'all values are {out[0]}')

output when all are on:

all values are on
  • Related