简体   繁体   中英

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

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

Here is an alternative solution:

df.eval('Col in @myValues')

Demo:

In [78]: pd.DataFrame(['A','B','B','A'],columns=['Col']).eval('Col in @myValues')
Out[78]:
0    True
1    True
2    True
3    True
dtype: bool

In [79]: pd.DataFrame(['A','A'],columns=['Col']).eval('Col in @myValues')
Out[79]:
0    True
1    True
dtype: bool

In [80]: pd.DataFrame(['B'],columns=['Col']).eval('Col in @myValues')
Out[80]:
0    True
dtype: bool

In [81]: pd.DataFrame(['B','C'],columns=['Col']).eval('Col in @myValues')
Out[81]:
0     True
1    False
dtype: bool

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM