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

Question:

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.

Asked By: Carola

||

Answers:

You can generate a random [0,1) float, then multiply by the upper bound and add 1:

import numpy as np

df['random'] = (df['Number'].mul(np.random.random(size=len(df))).astype(int)
                .add(1)
               )
print(df)

output:

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

edit: clip is actually not needed as random generates a [0,1) number.

Answered By: mozway
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.