检查dataseries列中的浮点数是否在另一列中的另一浮点数的.200之内

时间:2019-01-19 15:25:27

标签: python pandas

很抱歉,这个问题以前是否已经回答过,但我已经浏览了几个以前回答过的问题,无法自行解决。

到目前为止,我有:

df = pd.read_csv("CSVFILE.csv") 
df_close = pd.DataFrame(df.Close)

我的csv文件如下:

Open    High    Low     Close   Volume
133.795 137.610 132.905 137.275 165822.375

我想检查开盘价是否在收盘价的.200内。 即:如果“打开”为133.795,而“关闭”为133.995和133.595之间,则我希望程序告诉我。

非常感谢

2 个答案:

答案 0 :(得分:0)

使用Series.between

print (df)
      Open    High      Low    Close      Volume
0  133.795  137.61  132.905  137.275  165822.375
1  133.795  137.61  132.905  133.994  165822.375
2  133.795  137.61  132.905  133.596  165822.375

m = df['Close'].between(df['Open'] - .2, df['Open'] + .2)
print (m)
0    False
1     True
2     True
dtype: bool

或者:

a = df['Open'] - .2
b = df['Open'] + .2
m = (df['Close'] >= a ) & (df['Close'] <= b) 

答案 1 :(得分:0)

您也可以尝试以下操作:     将numpy导入为np

df.loc[:,'new_column'] = np.where((df['Open'] <= df['Close'] + 0.2) & (df['Open'] >= df['Close'] - 0.2), 'In range', 'Not in range')

仅过滤范围内的值:

df[df['new_column'] == 'In range']


Open    High    Low Close   Volume  new_column
1   133.795 137.61  132.905 133.994 165822.375  In range
2   133.795 137.61  132.905 133.596 165822.375  In range