vendredi 24 juillet 2020

Randomly select and assign values to given number of rows in python dataframe

How can I randomly select and assign values to given number of rows in python dataframe. Col B contains only 1's and 0's. Suppose I have a dataframe as below

Col A    Col B
  A        0
  B        0
  A        0
  B        0
  C        0
  A        0
  B        0
  C        0
  D        0
  A        0

I aim to randomly chose 5% of the rows and change the value of Col B to 1. I saw df.sample() but that wont allow me to do inplace changes to the column data




Aucun commentaire:

Enregistrer un commentaire