Say I have a pandas dataframe, with column A, and we want all the values in that column to be of type category with either L or R as the value.
How can I raise an exception if we detect that this column has any value other than L or R? That includes if it was None/null/NaN
>Solution :
We can filter on L and R and then we get the opposite of that filter using the ~ operator like so :
>>> df[~(df['A'].isin(['L', 'R']))]
To get a boolean saying that some additionnal values are present in the DataFrame, we can write :
>>> len(df[~(df['A'].isin(['L', 'R']))]) == 0