[英]How can I copy the value of a previous row, on the same column in a DataFrame?
I have the following DataFrame:我有以下 DataFrame:
Date![]() |
Track![]() |
Surface![]() |
FGrating![]() |
HorseId![]() |
Last FGrating at Sha Tin, grass![]() |
---|---|---|---|---|---|
2017-09-03 ![]() |
Sha Tin![]() |
Grass![]() |
110 ![]() |
1736 ![]() |
|
2017-09-16 ![]() |
Sha Tin![]() |
Grass![]() |
124 ![]() |
1736 ![]() |
110 ![]() |
2017-10-14 ![]() |
Sha Tin![]() |
Grass![]() |
118 ![]() |
1736 ![]() |
124 ![]() |
2017-11-11 ![]() |
Sha Tin![]() |
Grass![]() |
107 ![]() |
1736 ![]() |
118 ![]() |
2018-03-28 ![]() |
Happy Valley![]() |
Grass![]() |
117 ![]() |
1736 ![]() |
|
2018-04-11 ![]() |
Happy Valley![]() |
Grass![]() |
114 ![]() |
1736 ![]() |
|
2018-09-22 ![]() |
Sha Tin![]() |
Grass![]() |
124 ![]() |
1736 ![]() |
107 ![]() |
As you can see, there are spaces for the Last FGrating at Sha Tin, grass .如你所见,沙田的最后一次 FGrating,grass有空间。 These spaces have two causes:
这些空间有两个原因:
So far, I managed to do only the filling part, according to this code:到目前为止,根据这段代码,我只完成了填充部分:
def return_mask_and_text_from_tracks(data, track_no, metric):
if track_no == 0: # Sha Tin - grass
mask = (data.Track == 'Sha Tin') & (data.Surface == 'Grass')
text = str(metric) + ' at Sha-Tin Grass'
if track_no == 1: # Sha Tin - dirt
mask = (data.Track == 'Sha Tin') & (data.Surface == 'Dirt')
text = str(metric) + ' at Sha-Tin Dirt'
if track_no == 2: # Happy Valley - grass
mask = (data.Track == 'Happy Valley') & (data.Surface == 'Grass')
text = str(metric) + ' at Happy Valley Grass'
return mask, text
def compute_last_fgrating(data, mask=''):
if len(mask) == 0:
return data.groupby('HorseId')['FGrating'].apply(lambda x: x.shift(1))
else:
return data.loc[mask][['HorseId', 'FGrating']].groupby('HorseId')['FGrating'].apply(lambda x: x.shift(1))
for i in range(3):
mask, text = return_mask_and_text_from_tracks(featured_data, i, 'Last FGrating')
featured_data[text] = compute_last_fgrating(featured_data, mask=mask)
I am looking for ways to fill the rightmost column with the following values:我正在寻找用以下值填充最右边一列的方法:
The final table should look like this:决赛桌应如下所示:
Date![]() |
Track![]() |
Surface![]() |
FGrating![]() |
HorseId![]() |
Last FGrating at Sha Tin, grass![]() |
---|---|---|---|---|---|
2017-09-03 ![]() |
Sha Tin![]() |
Grass![]() |
110 ![]() |
1736 ![]() |
0 (no previous FGrating exists) ![]() |
2017-09-16 ![]() |
Sha Tin![]() |
Grass![]() |
124 ![]() |
1736 ![]() |
110 ![]() |
2017-10-14 ![]() |
Sha Tin![]() |
Grass![]() |
118 ![]() |
1736 ![]() |
124 ![]() |
2017-11-11 ![]() |
Sha Tin![]() |
Grass![]() |
107 ![]() |
1736 ![]() |
118 ![]() |
2018-03-28 ![]() |
Happy Valley![]() |
Grass![]() |
117 ![]() |
1736 ![]() |
118 (the lastFGrating on Sha Tin grass at this point) ![]() |
2018-04-11 ![]() |
Happy Valley![]() |
Grass![]() |
114 ![]() |
1736 ![]() |
118 ![]() |
2018-09-22 ![]() |
Sha Tin![]() |
Grass![]() |
124 ![]() |
1736 ![]() |
107 ![]() |
How can I do it?我该怎么做?
Here you only need在这里你只需要
df["Last FGrating at Sha Tin, grass"] = df["Last FGrating at Sha Tin, grass"].fillna(method="ffill").fillna(0)
The first fillna
fill NaN with the latest value available ( method="ffill"
) then you have left only one NaN on your first row and you can fill it with 0
.第一个
fillna
用可用的最新值( method="ffill"
)填充 NaN 然后你在第一行只剩下一个 NaN ,你可以用0
填充它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.