Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

randomly remove rows from dataframe based on condition

Tags:

python

pandas

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?

like image 655
user1934212 Avatar asked Jan 28 '17 16:01

user1934212


1 Answers

use sample + drop

df.drop(df.query('col1 < 6').sample(frac=.4).index)

   col1
1     2
3     4
4     5
5     6
6     7
7     8
8     9
9    10

For a range

df.drop(df.query('2 < col1 < 8').sample(frac=.4).index)

   col1
0     1
1     2
3     4
4     5
5     6
7     8
8     9
9    10
like image 187
piRSquared Avatar answered Oct 06 '22 05:10

piRSquared