Reputation: 1071
I have a pandas dataframe where I want to assign a random number to each row based on a value in the row and write out a dataframe.
So I'm trying:
for index, row in person[person['AGE_R'] == 1].iterrows():
row = index, random.randint(1, 15)
But I can't quite figure out how to write out a dataframe from it (not possible?). I been able to get out a list of tuples which I might be able to munge into a workable format, but I'm sure there's a better way.
I previously tried:
person[person['AGE_R'] == 1] = random.randint(1, 15)
But that sets all the 1's of 'AGE_R to what ever the randint is. Useful, but not what I'm looking for.
Any suggestions?
Thank you!
Upvotes: 1
Views: 140
Reputation: 117400
If you want to do vectorized operation, you can use numpy.random.randint:
>>> df = pd.DataFrame({'AGE_R':[1,2,3,5,4,3,1]})
>>> df
AGE_R
0 1
1 2
2 3
3 5
4 4
5 3
6 1
>>> df.ix[df['AGE_R'] == 1, 'AGE_R'] = np.random.randint(1, 15, len(df[df['AGE_R'] == 1]))
>>> df
AGE_R
0 5
1 2
2 3
3 5
4 4
5 3
6 11
Or you can use apply:
>>> df.ix[df['AGE_R'] == 1, 'AGE_R'] = df.ix[df['AGE_R'] == 1].apply(lambda x: np.random.randint(1, 15), axis = 1)
>>> df
AGE_R
0 5
1 2
2 3
3 5
4 4
5 3
6 12
Upvotes: 1