lundi 9 juillet 2018

Set N values to one at random positions in a zeros dataframe

In an (1,80) dataframe containing zeros, I want to set N (=30) values to one at random positions, so I tried this :

df = pd.DataFrame( 0, index = range(1), columns = range(80) )
df[ np.random.randint(80, size=30) ] = 1

but then if I check, I get 25 "ones" instead of 30 :

( df.loc[0] == 1 ).sum()
25

What is wrong and how can I fix this ?




Aucun commentaire:

Enregistrer un commentaire