pandas randomly replace k percent

前端 未结 3 938
轮回少年
轮回少年 2021-01-05 08:21

having a simple pandas data frame with 2 columns e.g. id and value where value is either 0 or 1 I would like

3条回答
  •  春和景丽
    2021-01-05 09:06

    you can probably use numpy.random.choice:

    >>> idx = df.index[df.value==1]
    >>> df.loc[np.random.choice(idx, size=idx.size/10, replace=False)].value = 0
    

提交回复
热议问题