I have a DataFrame in which the sum of the columns is 1, like so:
Out[]:
cod_mun ws_1 1_3 4 5_7 8 9_10 11 12 13 14 15 nd
1100015 0.1379 0.273 0.2199 0.1816 0.0566 0.0447 0.0617 0.0015 0 0.0021 0.0074 0.0137
1100023 0.1132 0.2009 0.185 0.2161 0.1036 0.0521 0.0885 0.0044 0.0038 0.0061 0.0181 0.0082
I want to draw a random number
import random
prob = random.random()
And then I want to compare such number with the cummulative sum of the columns from left to right and then return the columns' heading.
For example, if prob = 0.24 the threshold would reach 0.27 in the second column, 0.1379 + 0.273 > 0.24Then I would need to return the name of the column.
It it possible to do that WITHOUT a using 15 elifs?
Such that:
if prob < df.iloc[0]['ws_1']:
return 'ws_1'
elif prob < df.iloc[0]['ws_1'] + df.iloc[0]['1_3']
return '1_3'
elif ...
Aucun commentaire:
Enregistrer un commentaire