“Drop random rows” from pandas dataframe

后端 未结 2 1083
轮回少年
轮回少年 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:13

    Using drop with sample

    df.drop(df[df.Label.eq(1)].sample(2).index)
    
       Label   A
    0      0   1
    1      0   2
    2      0   3
    3      1  10
    5      1  12
    

提交回复
热议问题