简体   繁体   English

检查值是否在 Pandas dataframe 列中

[英]Check if value is in Pandas dataframe column

I've having an issue checking is a value is in the following dataframe:我有一个问题检查是一个值在以下 dataframe 中:

index指数 open打开 high高的 SignalEMA25M50M信号EMA25M50M PositionEMA25M50M位置EMA25M50M
2021-03-30 05:35:00 2021-03-30 05:35:00 0.000059 0.000059 0.000059 0.000059 0 0 -1.0 -1.0
2021-03-30 05:40:00 2021-03-30 05:40:00 0.000059 0.000059 0.000059 0.000059 0 0 0.0 0.0
2021-03-30 05:45:00 2021-03-30 05:45:00 0.000059 0.000059 0.000059 0.000059 0 0 0.0 0.0

i am trying to simply return true if the PositionEMA25M50M contains -1.0如果 PositionEMA25M50M 包含 -1.0,我试图简单地返回 true

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但是这每次都返回错误...我认为这与 PositionEMA25M50M 的类型为 float64 但我也尝试过

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.您不需要if循环。 You can directly use Series.eq with any to check if any row has -1 for this column:您可以直接将Series.eqany一起使用来检查该列是否有任何行具有-1

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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