Search code examples
pythonpandasintegerisin

Pandas isin equivalent for float or int


I have a dataframe with column A populated with numbers 1-9. I want to filter just on numbers 2 and 3. isin does not work for float dtypes. Is there an alternative?

something similar to:

df=df.loc[df['ColA'].isin([2,3])]

Solution

  • I think if need test 2.0, 3.0, 2, 3 your solution working well.

    df = pd.DataFrame({'ColA': [2.0, 6, -1, 3.0]})
    print (df.loc[df['ColA'].isin([2,3])])
       ColA
    0   2.0
    3   3.0
    

    If need convert floats to integers:

    df=df.loc[df['ColA'].astype(int).isin([2,3])]