简体   繁体   English

如何使用 groupby 在给定一些预定义条件的情况下确定字段的最后一个值?

[英]How can I determine the last value of a field given some predefined conditions using groupby?

I have the following data frame:我有以下数据框:

Track追踪 Surface表面 HorseId马匹编号 FGrating光栅 New FGrating新 FGrating
Sha Tin沙田 Grass 1736 1736 110 110 -1 -1
Sha Tin沙田 Grass 1736 1736 124 124 -1 -1
Sha Tin沙田 Grass 1736 1736 118 118 -1 -1
Happy Valley欢乐谷 Grass 1736 1736 117 117 -1 -1
Sha Tin沙田 Grass 13973 13973 144 144 -1 -1
Sha Tin沙田 Grass 13973 13973 137 137 -1 -1

I want to fill the New FGrating column with the last FGrating of every horse only if the track is Sha Tin and the surface is Grass.只有当赛道是沙田并且表面是草地时,我才想用每匹马的最后一个 FGrating 填充New FGrating列。 The rest of the columns can remain -1.其余的列可以保持 -1。 The result that I am looking for is this:我正在寻找的结果是这样的:

Track追踪 Surface表面 HorseId马匹编号 FGrating光栅 New FGrating新 FGrating
Sha Tin沙田 Grass 1736 1736 110 110 -1 -1
Sha Tin沙田 Grass 1736 1736 124 124 110 110
Sha Tin沙田 Grass 1736 1736 118 118 124 124
Happy Valley欢乐谷 Grass 1736 1736 117 117 -1 -1
Sha Tin沙田 Grass 13973 13973 144 144 -1 -1
Sha Tin沙田 Grass 13973 13973 137 137 144 144

For this, I tried to extract the required data intro a dataframe called temp .为此,我尝试在名为temp的数据帧中提取所需的数据。 The code I used is this:我使用的代码是这样的:

temp = featured_data.loc[(featured_data.Track == 'Sha Tin') & (featured_data.Surface == 'Gress')]
temp = temp[['HorseId', 'FGrating']]
temp = temp.groupby('HorseId')
temp['New FGrating'] = temp['FGrating'].apply(lambda x: x.shift(1))

The only problem this code has is that at the temp=temp.groupby('HorseId') line, the entire 'HorseId' column disappears.此代码的唯一问题是在temp=temp.groupby('HorseId')行,整个 'HorseId' 列消失了。

What am I doing wrong?我究竟做错了什么? How can I solve this problem?我怎么解决这个问题?

I can't check it myself at the moment but I think you can try it like this:我目前无法自己检查,但我认为您可以这样尝试:

temp = featured_data.loc[(featured_data.Track == 'Sha Tin') & (featured_data.Surface == 'Grass')]
temp = temp[['HorseId', 'FGrating']]
temp['New FGrating'] = temp.groupby('HorseId')['FGrating'].apply(lambda x: x.shift(1))

I think it is only a typo but you filtered for 'Gress' instead of 'Grass in your code example我认为这只是一个错字,但您在代码示例中过滤了“Gress”而不是“Grass”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在excel中使用python在某些条件下获取名称字段? - How to get the name field given some conditions in excel using python? 如何使用 groupby 确定到目前为止一列的平均值? - How can I determine the average of a column up to this point, using groupby? OpenERP 7:如何为字段的默认值设置条件? - OpenERP 7 : How can I make conditions for a field's default value? 在某些条件下,我可以减去 2 列熊猫吗 - Can i substract 2 column given some conditions pandas 在Django中,给定模型实例,如何确定ForeignKey字段是否已完全水合? - In Django, given a model instance, how can I determine if a ForeignKey field has been fully hydrated or not? 给定特定条件,如何使用 Pandas 和 groupby 计算一段时间内列的滚动平均值? - How can I compute the rolling mean of a column for a set period of time, using Pandas and groupby, given a specific condition? 基于groupby Python的第一个和最后一个值的条件创建一个新列 - Creating a new column based on conditions of first and last value of groupby Python 如何使用'groupby'提取数据 - How can I extract data using the 'groupby' 在某些条件下如何合并.csv文件? - How to merge .csv files given some conditions? 如何使用groupby执行此操作? - How can I do this by using groupby?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM