简体   繁体   中英

Filtering dataframes in pandas, how to chain multiple filters?

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? Eg something like:

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

或使用pandas.DataFrame.query

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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