简体   繁体   中英

Add a Rand to each row using Pandas' Assign and a Lambda Function

How do I pass a random number to a lambda function, so that I can be used by the pandas assign to add a different random number to each row.

My original attempt,

df = pd.DataFrame({'cat': [1]*10 + [0]*10,
                   'value': [3]*5 + [2]*5 + [2]*2 + [3]*8})

df.assign(cat=lambda df: df.cat + np.random.rand(1)).head(3)

Out[1]:
     cat        value
0   1.962857    3
1   1.962857    3
2   1.962857    3

We see here that the the random number 0.962857 has been added to all rows. But I would like a different rand for each row. How can I do this?

Change 1 , because return scalar to array by length of DataFrame :

print (np.random.rand(1))
[ 0.88642869]

print (np.random.rand(len(df)))

[ 0.42677701  0.89968857  0.87976326  0.07758206  0.43617027  0.03221375
  0.46398119  0.14226246  0.14237448  0.22679517  0.60271752  0.85003435
  0.5676184   0.87565266  0.89830548  0.27066452  0.23907483  0.73784657
  0.09083235  0.98984701]

df = df.assign(cat=lambda df: df.cat + np.random.rand(len(df))).head(3)
print (df)
        cat  value
0  1.886429      3
1  1.426777      3
2  1.899689      3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM