python - Pandas: How to extract rows of a dataframe matching Filter1 OR filter2 -


i have pandas dataframe exemple:

label          y88_n          diff       div      fold   0       25273.626713  17348.581851  2.016404  2.016404   1       29139.510491  -4208.868050  0.604304 -0.604304   2       34388.439717 -30147.834699  0.458903 -0.458903   3       69704.254089 -32976.152490  0.116894 -0.116894   4      193717.440783 -71359.494098  0.286045 -0.286045   5       28996.634708  10934.944533  2.031293  2.031293   6       45021.782930    680.437629  1.056383  1.056383   

but thousands of rows. new dataframe rows when values in 'fold' column > 2 or < 0.6. @ end dataframe should this:

label          y88_n          diff       div      fold   0       25273.626713  17348.581851  2.016404  2.016404   1       29139.510491  -4208.868050  0.604304 -0.604304   5       28996.634708  10934.944533  2.031293  2.031293 

i have tried different things like:

def ranged(start, end, step): x = start     while x < end:         yield x         x += step df2 = df[~df['fold'].isin(ranged(-0.6, 2, 0.000001))] 

or

df2 = df[(df['fold'] >= 2) & (df['fold'] <= -0.6)] 

but nothing seems work there easy way select values in column either matching filter 1 or filter 2? thanks

you do

in [276]: df[(df['fold'] >= 2) | (df['fold'] <= -0.6)] out[276]:    label         y88_n          diff       div      fold 0      0  25273.626713  17348.581851  2.016404  2.016404 1      1  29139.510491  -4208.868050  0.604304 -0.604304 5      5  28996.634708  10934.944533  2.031293  2.031293 

or use query method like

in [277]: df.query('fold >=2 | fold <=-0.6') out[277]:    label         y88_n          diff       div      fold 0      0  25273.626713  17348.581851  2.016404  2.016404 1      1  29139.510491  -4208.868050  0.604304 -0.604304 5      5  28996.634708  10934.944533  2.031293  2.031293 

and, pd.eval() works expressions containing large arrays

in [278]: df[pd.eval('df.fold >=2 | df.fold <=-0.6')] out[278]:    label         y88_n          diff       div      fold 0      0  25273.626713  17348.581851  2.016404  2.016404 1      1  29139.510491  -4208.868050  0.604304 -0.604304 5      5  28996.634708  10934.944533  2.031293  2.031293 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -