jeudi 1 février 2018

Compare elements and return values larger than random number as true

I'm trying to compare each unique variable in one array px to a random number in another array py. If the element in px is greater than or equal to that of py than I want to note that value as True.

Here's some code.

import pandas as pd
import random

px = np.array([0.360617,0.360617,0.360617,0.989699,0.989699,0.989699,-1.020482])
py = np.random.uniform(low=0, high=1, size=len(px))

df = pd.DataFrame({'px': px, 'py': py, 'status': px >= py})

The resultant dataframe looks like this:

         px        py  status
0  0.360617  0.509826   False
1  0.360617  0.129870    True
2  0.360617  0.818778   False
3  0.989699  0.953721    True
4  0.989699  0.740662    True
5 -1.020482  0.302593    False

But I need it to look something like this:

         px        py  status
0  0.360617  0.509826   False
1  0.360617  0.509826   False
2  0.360617  0.509826   False
3  0.989699  0.953721    True
4  0.989699  0.953721    True
5 -1.020482  0.302593    False

whereby each unique px has an associated py. I'd like to see an example doing this with a for-loop and some other method such as groupby.




Aucun commentaire:

Enregistrer un commentaire