Pandas groupby - 计算与相对点的距离 [英] Pandas groupby - calculating distance from relative point

查看:45
本文介绍了Pandas groupby - 计算与相对点的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个看起来像这样的东西

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

我想要做的是创建一个新列,它是 Number 与 ref 中 True 的差异.因此,对于 A 组,True 是最后一个,因此该列将显示为 -4,-3,-2,-1,0.我一直在考虑做以下事情:

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)

这似乎为每个组正确计算,但我不确定如何将结果加入 df.

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

推荐答案

您可以执行以下操作:

df["new"] = df.Number - df.Number[df.groupby('Event')['Ref'].transform('idxmax')].reset_index(drop=True)
print(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

这个:df.groupby('Event')['Ref'].transform('idxmax') 填充按组查找索引,其中 Ref 为 True.基本上它会找到最大值的索引,因此假设 True = 1 和 False = 0,它会找到 True 值的索引.

This: df.groupby('Event')['Ref'].transform('idxmax') fill find the indices by group where Ref is True. Basically it finds the indices of the max values, so given that True = 1, and False = 0, it find the indices of the True values.

这篇关于Pandas groupby - 计算与相对点的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆