Categories
pandas python

randomly remove rows from dataframe based on condition

given a dataframe with numerical values in a specific column, I want to randomly remove a certain percentage of the rows for which the value in that specific column lies within a certain range.

For example given the following dataframe:

df = pd.DataFrame({'col1': [1,2,3,4,5,6,7,8,9,10]})
df
col1
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

2/5 of the rows where col1 is below 6 should be removed randomly.

Whats the most concise way to do that?