0

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)]
Ptit Xav
  • 3,006
  • 2
  • 6
  • 15
a7dc
  • 3,323
  • 7
  • 32
  • 50
  • 1
    Does this answer your question? [pandas: multiple conditions while indexing data frame - unexpected behavior](https://stackoverflow.com/questions/22591174/pandas-multiple-conditions-while-indexing-data-frame-unexpected-behavior) – Ynjxsjmh May 15 '22 at 10:59

1 Answers1

1
df = data[(data['change'] >= 10) & (open <= 15) & (open >= 1)]

or using pandas.DataFrame.query:

df = data.query("1 <= open <= 15 and change >= 10")
anras5
  • 51
  • 2