Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply random.uniform to two columns of a dataframe?

How to apply random.uniform to two columns of a dataframe?

df = pd.DataFrame(np.random.randn(6,4),columns=list('ABCD'))

def f(x):
    return random.uniform(x[0],x[1])

df['E'] = f(df[['A','B']])
like image 371
Martin Avatar asked Nov 18 '25 07:11

Martin


1 Answers

A vectorized approach would be:

df['E'] = (df.B - df.A) * np.random.rand(df.shape[0]) + df.A

Same as:

df['E'] = (df.B - df.A) * np.random.uniform(size=df.shape[0]) + df.A

Timing 1 million rows

Don't use apply over large datasets if you can help it.

enter image description here

like image 95
piRSquared Avatar answered Nov 19 '25 20:11

piRSquared