Mina Baqerzade
Mina Baqerzade

Reputation: 51

Why "isin" does not work for my DataFrame

This is a part of my DataFrame

          0                   1
0 -0.045988 2018-01-01 00:00:00
1 -0.046024 2018-01-01 00:01:00
2 -0.044360 2018-01-01 00:02:00
3 -0.044722 2018-01-01 00:03:00
4 -0.043637 2018-01-01 00:04:00

for example, I want to find the -0.043637 in DataFrame:

X.isin([-0.043637])

but the result is like

       0      1
0  False  False
1  False  False
2  False  False
3  False  False
4  False  False

The type of DataFrame is

0           float64
1    datetime64[ns]
dtype: object

It is really strang that it does not work and return the wrong answer. What's your idea, why it happened?

Upvotes: 0

Views: 667

Answers (1)

DOOM
DOOM

Reputation: 1244

Comparing float is complicated and to simplify, you have to provide precision.

so, instead your code can be written as,

df[0].apply(lambda x: np.isclose(x, -0.043637, atol=1e-5))

For performance, you can convert the float to str when comparing,

df[0].astype(str).apply(lambda x: x.startswith("-0.043637"))

Upvotes: 1

Related Questions