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)>>> dfLabel Value 1 Value 20 a 0 561 b 9 342 c 3 993 d 7 114 e 2 785 f 6 896 g 5 347 h 9 948 i 6 659 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)]>>> dfLabel Value 1 Value 22 c 3 993 d 7 115 f 6 896 g 5 348 i 6 659 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)]>>> dfLabel Value 1 Value 26 g 5 348 i 6 659 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 |
