4

I'm looking for a way to create a random dataframe with 3 columns and 3 rows, but from which the random numbers of the first column should be in the range [1:5], the second in [1:8] and the third in [4:10].

Example from what I need:

   A  B  C
0  4  3  9
1  1  4  10
2  4  8  4

I know how to create a random dataframe with:

df = pd.DataFrame(np.random.randint(1,10,size=(3, 3)))

But I can't find a way of adding these ranges as a condition per column. I can generate three random numbers and then add them together as dataframe, but that is not what I'm looking for.

Any help?

1 Answer 1

4
n = 3
df = pd.DataFrame(dict(
    A=np.random.randint(1, 6, size=n),
    B=np.random.randint(1, 9, size=n),
    C=np.random.randint(4, 11, size=n)
))

df

   A  B  C
0  3  5  6
1  1  7  6
2  1  1  4

Or

df = pd.DataFrame(
    np.random.rand(3, 3) * [5, 8, 7] + [1, 1, 4],
    columns=list('ABC')
).astype(int)

df

   A  B   C
0  3  6  10
1  3  5   7
2  4  6   7
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.