usert4jju7
usert4jju7

Reputation: 1813

Filter dataframe

I'm using Python 3.9.

I'm trying to filter a pandas dataframe using np.where (np being numpy) but unable to do so. I don't understand what mistake I'm making.

Please could I request some help.

Code

import pandas as pd
import numpy as np

result_data = pd.read_csv("test_data.csv")
print(result_data)

print(np.where(result_data['candle_high'] >= result_data['ema_close'] >= result_data['candle_low'], True, False))

I do see there are values in the dataframe that match the condition of ema_close being between candle_high & candle_low

Online Jupyter notebook link

Upvotes: 0

Views: 202

Answers (3)

Nisit
Nisit

Reputation: 71

try

(df['A'] + df['B']).where((df['A'] < 0) | (df['B'] > 0), df['A'] / df['B'])

I think np.where(A, B) for A,B is numpy array. see more https://stackoverflow.com/a/38579700/3981296

Upvotes: 1

Anurag Dabas
Anurag Dabas

Reputation: 24304

you can also try:

via between() method:

mask=result_data['ema_close'].between(result_data['candle_high'],result_data['candle_low'])

print(mask)

Upvotes: 1

Scott Boston
Scott Boston

Reputation: 153460

IIUC,

results_data.query('candle_high <= ema_close <= candle_low')

Upvotes: 1

Related Questions