[英]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![]() |
---|---|---|---|---|
Sha Tin![]() |
Grass![]() |
1736 ![]() |
110 ![]() |
-1 ![]() |
Sha Tin![]() |
Grass![]() |
1736 ![]() |
124 ![]() |
-1 ![]() |
Sha Tin![]() |
Grass![]() |
1736 ![]() |
118 ![]() |
-1 ![]() |
Happy Valley![]() |
Grass![]() |
1736 ![]() |
117 ![]() |
-1 ![]() |
Sha Tin![]() |
Grass![]() |
13973 ![]() |
144 ![]() |
-1 ![]() |
Sha Tin![]() |
Grass![]() |
13973 ![]() |
137 ![]() |
-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![]() |
---|---|---|---|---|
Sha Tin![]() |
Grass![]() |
1736 ![]() |
110 ![]() |
-1 ![]() |
Sha Tin![]() |
Grass![]() |
1736 ![]() |
124 ![]() |
110 ![]() |
Sha Tin![]() |
Grass![]() |
1736 ![]() |
118 ![]() |
124 ![]() |
Happy Valley![]() |
Grass![]() |
1736 ![]() |
117 ![]() |
-1 ![]() |
Sha Tin![]() |
Grass![]() |
13973 ![]() |
144 ![]() |
-1 ![]() |
Sha Tin![]() |
Grass![]() |
13973 ![]() |
137 ![]() |
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.