简体   繁体   中英

Check if value is in Pandas dataframe column

I've having an issue checking is a value is in the following dataframe:

index open high SignalEMA25M50M PositionEMA25M50M
2021-03-30 05:35:00 0.000059 0.000059 0 -1.0
2021-03-30 05:40:00 0.000059 0.000059 0 0.0
2021-03-30 05:45:00 0.000059 0.000059 0 0.0

i am trying to simply return true if the PositionEMA25M50M contains -1.0

i have tried:

if -1.0 in indicator_5min_df.PositionEMA25M50M:
    print('true')
else:
    print('false')

however this returns false every time... i assume this is something to do with PositionEMA25M50M being of type float64 however i've also tried

if np.float64(-1.0) in indicator_5min_df.PositionEMA25M50M:
    print('true')
else:
    print('false')

which has given me the same result...

any ideas how i can fix this?

You don't need a if loop. You can directly use Series.eq with any to check if any row has -1 for this column:

In [990]: df['PositionEMA25M50M'].eq(-1).any()
Out[990]: True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM