Student T Student T - 2 months ago 5
Python Question

How to compare dataframe unique values with a list?

I have a Panda dataframe column, and I want to check if all values in my column come from another list.

For example, I want to check whether all values in my column are

A
or
B
and nothing else. My code should return true for the following inputs:

myValues = ['A','B']
df = pd.DataFrame(['A','B','B','A'],columns=['Col']) # True
df = pd.DataFrame(['A','A'],columns=['Col']) # True
df = pd.DataFrame(['B'],columns=['Col']) # True
df = pd.DataFrame(['B','C'],columns=['Col']) # False

Answer

You can use isin and pass your list to generate a boolean array and with all to return whether all values present:

In [146]:    
myValues = ['A','B']
df = pd.DataFrame(['A','B','B','A'],columns=['Col']) # True
print(df['Col'].isin(myValues).all())
df = pd.DataFrame(['A','A'],columns=['Col']) # True
print(df['Col'].isin(myValues).all())
df = pd.DataFrame(['B'],columns=['Col']) # True
print(df['Col'].isin(myValues).all())
df = pd.DataFrame(['B','C'],columns=['Col']) # False
print(df['Col'].isin(myValues).all())


True
True
True
False
Comments