1

I have a dataframe df:

import pandas as pd
d = {'cola': ['a1', 's7', 'm5']}
df = pd.DataFrame(d)
df

    cola
0     a1
1     s7
2     m5

The output of the code below is 1 but it should be 0 because s7 in cola is not in ['a1', 'r3', 'm5']:

if ~df["cola"].isin(['a1', 'r3', 'm5']).any():
    print(0)
else:
    print(1)

What the code should do is print 0 if any value of Column A (cola) is not of the type a1, r3 or m5.

williamscathy825
  • 185
  • 1
  • 1
  • 11
  • Why do you add `~` in front of `df['cola']`? `not any` means all of them are not in right? – Quang Hoang Mar 26 '20 at 02:11
  • @Quang Hoang: to check that if any value in cola is not of the type a1, r3 or m5 – williamscathy825 Mar 26 '20 at 02:13
  • 1
    In this case `cola` does contain `a1` at the first place. So `df['cola'].isin(['a1','r3','m5']).any()` is `True`. Therefore negate of that is `False`. It seems you want to check that **none** of `cola` is in `['a1','r3','m5']`. In that case, you would want to use `~df['cola'].isin(['a1','r3','m5']).all()`? – Quang Hoang Mar 26 '20 at 02:15
  • @Quang Hoang: i think i didn't understand the question sorry! i want to print 0 if any of the values in cola column is not 'a1' or 'r3' or 'm5' that is if of any other type – williamscathy825 Mar 26 '20 at 02:18
  • That's basic logic, `any is not in == not all is in`. Try my code and see. – Quang Hoang Mar 26 '20 at 02:20
  • @Quang Hoang: ok let me try thanks – williamscathy825 Mar 26 '20 at 02:21

1 Answers1

1

The way it works it checks if any of the "cola" values is in (['a1', 'r3', 'm5']), and it is true, so df["cola"].isin(['a1', 'r3', 'm5']).any() is True. Then you are negating it and it takes you to else:. You need to use all(). Cheers

griggy
  • 446
  • 4
  • 7