A solution to delete rows with values below and above a minimum and maximum value in a pandas data frame is to use the function between(). Let's consider the following data frame
>>> import pandas as pd
>>> data = {'Label':['a','b','c','d','e','f','g','h','i','j'],'Value 1':[0,9,3,7,2,6,5,9,6,3], 'Value 2':[56,34,99,11,78,89,34,94,65,36]}
>>> df = pd.DataFrame(data)
>>> df
Label Value 1 Value 2
0 a 0 56
1 b 9 34
2 c 3 99
3 d 7 11
4 e 2 78
5 f 6 89
6 g 5 34
7 h 9 94
8 i 6 65
9 j 3 36
To remove rows from the column called "Value 1" that are not between vmin and vmax:
>>> vmin = 3
>>> vmax = 7
>>> column = 'Value 1'
a solution is to use between:
>>> df = df[df[column].between(vmin, vmax)]
>>> df
Label Value 1 Value 2
2 c 3 99
3 d 7 11
5 f 6 89
6 g 5 34
8 i 6 65
9 j 3 36
Now using the function between on the column "Value 2" with:
>>> vmin = 20
>>> vmax = 80
>>> column = 'Value 2'
returns
>>> df = df[df['Value 2'].between(20, 80)]
>>> df
Label Value 1 Value 2
6 g 5 34
8 i 6 65
9 j 3 36
References
Links | Site |
---|---|
between | pandas.pydata.org |
How to select rows in a DataFrame between two values, in Python Pandas? | stackoverflow |
How to exclude values from pandas dataframe? | stackoverflow |
pandas.DataFrame.clip | pandas.pydata.org |
Select rows from a DataFrame based on values in a column in pandas | stackoverflow |
Best way to subset a pandas dataframe | stackoverflow |
Indexing and Selecting Data with Pandas | geeksforgeeks |
Python Pandas DataFrame.where() | geeksforgeeks |