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>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.isin.html" rel="nofollow"><code>isin</code></a> and pass your list to generate a boolean array and with <code>all</code> to return whether all values present:</p>

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

python pandas