Reputation: 1352
Hi I have the following data frames:
import numpy as np
import pandas as pd
df = pd.DataFrame()
df['T1'] = ['A','B','C','D','E']
df['T2'] = ['G','H','I','J','K']
df['Match'] = df['T1'] +' Vs '+ df['T2']
Nsims = 5
df1 = pd.DataFrame((pd.np.tile(df,(Nsims,1))))
I created two new columns T1_point and T2_point by summing of five random numbers. when I do as follow: it gave me the same number for all rows.
Ninit = 5
df1['T1_point'] = np.sum(np.random.uniform(size=Ninit))
df1['T2_point'] = np.sum(np.random.uniform(size=Ninit))
What I wanted to do is that I would like to get different values for each row by using random number.
How could I do that?
Thanks
Zep.
Upvotes: 3
Views: 14538
Reputation: 1028
The list comprehension answer is not optimal.
A simpler and more efficient answer is to use the size parameter available in numpy which creates a matching array:
import numpy as np
df1['RAND'] = np.random.randint(1,10000000, size=len(df1))
Upvotes: 2
Reputation: 2670
What you are basically asking is for a random number in each row. Just create a list of random numbers then and append them to your dataframe?
import random
df1['RAND'] = [ random.randint(1,10000000) for k in df1.index]
print df1
0 1 RAND
0 A G 6850189
1 B H 3692984
2 C I 8062507
3 D J 6156287
4 E K 7037728
5 A G 7641046
6 B H 1884503
7 C I 7887030
8 D J 4089507
9 E K 4253742
10 A G 8947290
11 B H 8634259
12 C I 7172269
13 D J 4906697
14 E K 7040624
15 A G 4702362
16 B H 5267067
17 C I 3282320
18 D J 6185152
19 E K 9335186
20 A G 3448703
21 B H 6039862
22 C I 9884632
23 D J 4846228
24 E K 5510052
Upvotes: 10