Home > Software design >  How to randomly fill X of rows in a pandas dataframe?
How to randomly fill X of rows in a pandas dataframe?

Time:11-28

How to randomly fill the rows of a dataframe by setting a number? For example:

Given a pandas dataframe with 10 elements:

col1
a
b
c
d
e
f
g
h
i
j

How to fill randomly with 1 and the rest with 0 in the rows of another column. For example, I would like to fill four rows with 1 and the rest six 0:

col1 col2
a     1
b     0
c     1
d     1
e     0
f     1
g     0
h     0
i     0
j     0

CodePudding user response:

This should do the trick. For each row, set the col2 to a random int between 0 and 1

df["col2"] = df.apply(lambda x: randint(0,1), axis=1)

If you need n random values to exist and the rest to be set, you can try this:

n = 4
df["col2"] = 0
df_to_update = df.sample(n)
df_to_update = 1
df.update(df_to_update)
  • Related