Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas groupby - calculating distance from relative point

Lets say I have something that looks like this

df = pd.DataFrame({'Event':['A','A','A','A', 'A' ,'B','B','B','B','B'],  'Number':[1,2,3,4,5,6,7,8,9,10],'Ref':[False,False,False,False,True,False,False,False,True,False]})

What I want to do is create a new column which is the difference in Number from the True in ref. So for the A group, the True is the last one, so the column would read -4,-3,-2,-1,0. I have been thinking to do the following:

for col in df.groupby('Event'):
    temp = col[1]
    reference = temp[temp.Ref==True]
    dist1 = temp.apply(lambda x:x.Number-reference.Number,axis=1)

This seems to correctly calculate for each group, but I am not sure how to join the result into the df.

like image 330
yankeefan11 Avatar asked Nov 20 '25 02:11

yankeefan11


1 Answers

In your case

df['new']=(df.set_index('Event').Number-df.query('Ref').set_index('Event').Number).to_numpy()
df
  Event  Number    Ref  new
0     A       1  False   -4
1     A       2  False   -3
2     A       3  False   -2
3     A       4  False   -1
4     A       5   True    0
5     B       6  False   -3
6     B       7  False   -2
7     B       8  False   -1
8     B       9   True    0
9     B      10  False    1
like image 79
BENY Avatar answered Nov 22 '25 14:11

BENY