How to compare dataframe unique values with a list?
<p>I have a Panda dataframe column, and I want to check if all values in my column come from another list.</p>

<p>For example, I want to check whether all values in my column are <code>A</code> or <code>B</code> and nothing else. My code should return true for the following inputs:</p>

<pre><code>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
</code></pre>

<p>Here is an alternative solution:</p>

<pre><code>df.eval('Col in @myValues')
</code></pre>

<p>Demo:</p>

<pre><code>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
</code></pre>

python pandas