jeudi 27 octobre 2022

Generate a random number for each row dependent on another column Pandas DataFrame

Let it be the following Dataframe of pandas in Python.

Column_1 Column_2 Number
UDKA 1234 5
MAKU 1544 5
POKA 5434 2
UJFK 9104 3

I want to generate a random number column that generates for each row a random number between 1 and its value in the Number column df['Random'] = rand(1, x.Number). Example result:

Column_1 Column_2 Number Random
UDKA 1234 5 4
MAKU 1544 5 2
POKA 5434 2 1
UJFK 9104 3 2

Obviously Random cannot be strictly greater than Number.




Aucun commentaire:

Enregistrer un commentaire