“Drop random rows” from pandas dataframe

后端 未结 2 1052
轮回少年
轮回少年 2021-01-20 21:43

In a pandas dataframe, how can I drop a random subset of rows that obey a condition?

In other words, if I have a Pandas dataframe w

2条回答
  •  清酒与你
    2021-01-20 22:26

    Use the frac argument

    df.sample(frac=.5)
    

    If you define the amount you want to drop in a variable n

    n = .5
    df.sample(frac=1 - n)
    

    To include the condition, use drop

    df.drop(df.query('Label == 1').sample(frac=.5).index)
    
       Label   A
    0      0   1
    1      0   2
    2      0   3
    4      1  11
    6      1  13
    

提交回复
热议问题