vp7
vp7

Reputation: 388

Create a column with fixed values

I am trying to create a new column in dataframe with values :

data = [4.91,4.93,5.02,4.93,4.82,4.57,4.49,4.57,4.54,4.52,4.56,4.73]

I have more than 50,000 rows in the dataframe and I want the values to be assigned randomly to the new column.

so the idea is that these values would be assigned randomly and repeated in the column.

I was thinking of using lambda function with this logic :

df.assign(value=lambda x: #function here)

Can anyone suggest any other way or a simpler way for the same? I am not able to understand the logic the function for assigning the values randomly.

Thanks

Upvotes: 2

Views: 196

Answers (1)

jezrael
jezrael

Reputation: 862641

Use numpy.random.choice with length of DataFrame:

import numpy as np

df = pd.DataFrame({
         'A':[7,8,9,4,2,3],
})

data = [4.91,4.93,5.02,4.93,4.82,4.57,4.49,4.57,4.54,4.52,4.56,4.73]

df = df.assign(value=np.random.choice(data, len(df)))
print (df)
   A  value
0  7   4.93
1  8   4.91
2  9   4.54
3  4   4.49
4  2   4.56
5  3   4.82

Upvotes: 4

Related Questions