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

Jérémz

I have a pandas dataframe that look like this for 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 with thousands of rows. I would like to get a new dataframe with rows when values in 'fold' column are > 2 OR < 0.6. So at the end the dataframe should look like 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 to work Is there a easy way to select values in a column that are either matching a filter 1 OR a filter 2? Thanks

Zero

You could 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 well with 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to filter a pandas dataframe by string values and matching integers in rows?

From Dev

How to filter Pandas rows by another Dataframe columns?

From Dev

Writing python function to extract matching rows from pandas dataframe

From Dev

pandas DataFrame filter by rows and columns

From Java

How to filter rows containing a string pattern from a Pandas dataframe

From Dev

How do you filter rows in a pandas dataframe conditional on columns existing?

From Dev

How to filter a pandas dataframe to just the rows that show change within a group?

From Dev

How to filter rows that fall within 1st and 3rd quartile of a particular column in pandas dataframe?

From Dev

Python Pandas - how to apply boolean series to extract rows from dataframe

From Dev

how to extract pandas series element and compare it with rows in dataframe's column

From Java

pandas: filter rows of DataFrame with operator chaining

From Dev

Filter pandas dataframe rows by multiple column values

From Dev

Pandas filter dataframe rows with a specific year

From Dev

pandas dataframe enumerate rows that passed a filter

From Dev

SQL Server - How to filter rows based on matching rows?

From Dev

How to filter values by Column Name and then extract the rows that have the same value to another CSV file? Python/Pandas

From Dev

How to iterate through selected rows in pandas dataframe with conditions matching three rows?

From Dev

Filter the duplicate rows of a pandas dataframe, keeping rows with the latest date only

From Dev

dealing with huge number of rows (pandas dataframe) to filter rows on condition

From Dev

filter multiple separate rows in a DataFrame that meet the condition in another DataFrame with pandas?

From Dev

Python/Pandas: filter and organize the rows and columns of a dataframe based on another dataframe

From Dev

Extract rows with maximum values in pandas dataframe

From Dev

How to extract files and filter conditions from DataFrame?

From Dev

Filter DataFrame rows that are lists

From Dev

Filter rows of pandas dataframe whose values are lower than 0

From Dev

Regular expression to filter desired rows from pandas dataframe

From Dev

Filter pandas dataframe based on a column: keep all rows if a value is that column

From Dev

How to Extract Rows of Data into a DataFrame using Selenium

From Java

How to filter rows with specified conditions in a dataframe and put them in a new dataframe?

Related Related

  1. 1

    How to filter a pandas dataframe by string values and matching integers in rows?

  2. 2

    How to filter Pandas rows by another Dataframe columns?

  3. 3

    Writing python function to extract matching rows from pandas dataframe

  4. 4

    pandas DataFrame filter by rows and columns

  5. 5

    How to filter rows containing a string pattern from a Pandas dataframe

  6. 6

    How do you filter rows in a pandas dataframe conditional on columns existing?

  7. 7

    How to filter a pandas dataframe to just the rows that show change within a group?

  8. 8

    How to filter rows that fall within 1st and 3rd quartile of a particular column in pandas dataframe?

  9. 9

    Python Pandas - how to apply boolean series to extract rows from dataframe

  10. 10

    how to extract pandas series element and compare it with rows in dataframe's column

  11. 11

    pandas: filter rows of DataFrame with operator chaining

  12. 12

    Filter pandas dataframe rows by multiple column values

  13. 13

    Pandas filter dataframe rows with a specific year

  14. 14

    pandas dataframe enumerate rows that passed a filter

  15. 15

    SQL Server - How to filter rows based on matching rows?

  16. 16

    How to filter values by Column Name and then extract the rows that have the same value to another CSV file? Python/Pandas

  17. 17

    How to iterate through selected rows in pandas dataframe with conditions matching three rows?

  18. 18

    Filter the duplicate rows of a pandas dataframe, keeping rows with the latest date only

  19. 19

    dealing with huge number of rows (pandas dataframe) to filter rows on condition

  20. 20

    filter multiple separate rows in a DataFrame that meet the condition in another DataFrame with pandas?

  21. 21

    Python/Pandas: filter and organize the rows and columns of a dataframe based on another dataframe

  22. 22

    Extract rows with maximum values in pandas dataframe

  23. 23

    How to extract files and filter conditions from DataFrame?

  24. 24

    Filter DataFrame rows that are lists

  25. 25

    Filter rows of pandas dataframe whose values are lower than 0

  26. 26

    Regular expression to filter desired rows from pandas dataframe

  27. 27

    Filter pandas dataframe based on a column: keep all rows if a value is that column

  28. 28

    How to Extract Rows of Data into a DataFrame using Selenium

  29. 29

    How to filter rows with specified conditions in a dataframe and put them in a new dataframe?

HotTag

Archive