3

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.

6
  • 1
    Why are you summing them if you want different values? This makes no sense? Commented Jun 27, 2018 at 4:30
  • Please provide a Minimal, Complete, and Verifiable example Commented Jun 27, 2018 at 4:34
  • @coldspeed, I am trying to replicate the penalty shoot process.That's why I used 5 random number and sum it so that i can decide who will win Commented Jun 27, 2018 at 4:35
  • I did not understand. So you want to take 25 numbers and sum them in groups of 5? Commented Jun 27, 2018 at 4:38
  • I am simulating for 5 times for each match Commented Jun 27, 2018 at 4:39

2 Answers 2

11

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

3 Comments

random.randint() doesn't guarantee unique values always
@Mohan Babu, then he could just use a range, or even use the index for uniqueness. I dont really understand what he is doing to be honest.
Thanks ajsp. I could extend the process using your advice.
2

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))

Comments

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.