2

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

1
  • df['value'] = pd.np.random.choice(data, df.shape[0])? Commented Sep 6, 2019 at 6:01

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

This is putting other values as well instead of the values mentioned in array. Any reason as to why this is happening?
@vp7 - hmmm, I think not possible, only reason should be float accuracy of values in list - so seems values are different.

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.