Maneet Giri Maneet Giri - 7 months ago 88
Python Question

Filtering on Boolean column using pandas but got FutureWarning

I matched 2 columns in a DataFrame and yield the result in Boolean value in a new 'bool' column.
First I wrote:

df_new = df[[7 for 32 in df if df 39 == 'False']]


But it din't work.

Then I wrote only to match the columns

My code is

df['bool'] = (df.iloc[:, 7] == df.iloc[:, 32])


The above code matched the columns positioned at 7 and 32 and placed
bool
value at column 39. In this I want to slice the data and choose the rows with a false value.
Data

I wrote:

df_filtered = df[df['bool'] == 'False']


but I got the Future error as


c:\python34\lib\site-packages\pandas\core\ops.py:714: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
result = getattr(x, name)(y)



Not sure what wrong I am doing.
I've also tried

df[df[39] == 'False']

Answer

You can compare the columns directly:

df = pd.DataFrame({'a': [1, 2, 3], 'b': [1, 3, 2]})

df['Bool'] = df.a == df.b

>>> df
   a  b   Bool
0  1  1   True
1  2  3  False
2  3  2  False

To filter for False values, use the negation flag, i.e. ~:

>>> df[~df.Bool]
   a  b   Bool
1  2  3  False
2  3  2  False