Home > Software engineering >  Creating a column with random values between 5 distinct floats or strings
Creating a column with random values between 5 distinct floats or strings

Time:11-14

Good morning

I need to create two dataframe columns.

First one shall have random values that are either 0,05 0.10, 0.15 0.20 or 0.25.

I have tried using:

np.random.uniform

but this returns me unwanted values such as 0.07 or 0.12.

I also have another column for which I want to do the same("Assigning random values") but with strings : "Positive" or "Negative"

Desired ouput:
Hg      Outcome
0.15  Positive
0.10  Positive
0.20  Negavtive
...
...

Thank you

CodePudding user response:

You can use numpy.random.choice:

n = 20

df = pd.DataFrame({'Hg': np.random.choice([0.05, 0.10, 0.15, 0.20, 0.25], size=n),
                   'Outcome': np.random.choice(['Positive', 'Negative'], size=n)
                   })

print(df)

Example output:

      Hg   Outcome
0   0.25  Negative
1   0.20  Positive
2   0.25  Positive
3   0.10  Positive
4   0.10  Positive
5   0.10  Positive
6   0.20  Negative
7   0.05  Negative
8   0.15  Positive
9   0.15  Negative
10  0.20  Negative
11  0.20  Positive
12  0.05  Positive
13  0.20  Negative
14  0.15  Positive
15  0.15  Negative
16  0.05  Positive
17  0.20  Negative
18  0.10  Negative
19  0.20  Positive
  • Related