Skip to content Skip to sidebar Skip to footer

Return Dataframe With Values In A Particular Range For All Columns

How do I return a dataframe that has values in a particular range for all the columns. My dataframe is currently structured like this: California Texas New York ...

Solution 1:

I hope this might be your solution:

df['California'].between(150000, 200000, inclusive=False)

Here, inclusive determines that whether you want to include the edges or not. True is equal to <= and >= while, False means < or >

Solution 2:

>>>df
     0    1    2    3    4
0   33  131   52  122   40
1  235  146   36    4   97
2   90  227   49   49   58
3  192   61  127  220  254
4  124  234  238  215   34
5   86    3  220  105  129
6   59  234  189  193  190
7  116  131   95   89  102
8   72   90  253  167  203
9   21  111  203   55  118

Define the condition/criteria

>>>gt = df > 100>>>lt = df < 150>>>mask = np.logical_and(lt,gt)

Use .any(axis=1) if any column can meet the criteria

>>> mask.any(1)
0True1True2False3True4True5True6False7True8False9True
dtype: bool>>> df[mask.any(1)]
     01234033131521224012351463649731926112722025441242342382153458632201051297116131958910292111120355118

If all the columns have to meet the criteria use .all(axis=1)

>>> lt = df < 180>>> gt = df > 30>>> mask = np.logical_and(lt,gt)
>>> mask.all(1)
0True1False2False3False4False5False6False7True8False9False
dtype: bool>>> df[mask.all(1)]
     01234033131521224071161319589102>>> 

Post a Comment for "Return Dataframe With Values In A Particular Range For All Columns"