I have a function which filters my data, like so:
def get_gappers(data):
data = pd.read_csv(data)
open = data['open']
df = data[(data['change'] >= 10)]
df = df[(open <= 15)]
df = df[(open >= 1)]
display(df)
Which gives me the following:
date volume open close high low previous close change
214 2022-03-08 88067102.0 13.035 13.51 14.27 12.4401 12.84 11.137072
219 2022-03-15 76398350.0 14.910 15.57 15.80 14.9000 14.25 10.877193
My question is how do I do the filtering using a single line? E.g something like:
df = data[(data['change'] >= 10) & open <= 15 open >= 1)]
CodePudding user response:
df = data[(data['change'] >= 10) & (open <= 15) & (open >= 1)]
or using pandas.DataFrame.query
:
df = data.query("1 <= open <= 15 and change >= 10")