TransWikia.com

pandas isin() fail between dataframes with objects types columns

Stack Overflow Asked by Franco Milanese on January 5, 2022

In [1]: import pandas as pd
   ...: a=pd.DataFrame([1,2,'a'])

In [2]: a.isin([1,'a'])
Out[2]:
       0
0   True
1  False
2   True

In [3]: a.isin(pd.DataFrame([1,'a']))
Out[3]:
       0
0   True
1  False
2  False

why isin cant find ‘a’ in a DataFrame but can in a list?.

PS: Using pandas 1.0.5

In [4]: pd.version
Out[4]: ‘1.0.5’

One Answer

The pd.DataFrame.isin docs spell out this behavior pretty clearly, emphasis my own.

If values is a DataFrame, then both the index and column labels must match.

So looking at your two DataFrames side by side:

a   isin  pd.DataFrame([1,'a'])

   0           0
0  1        0  1    True  <- 1 == 1 for col label (0) and index label (0) 
1  2        1  a    False <- 2 != 'a' for col label (0) and index label (1)
2  a                False <- Nothing to align 

Answered by ALollz on January 5, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP