简体   繁体   English

如何在 DataFrame 的同一列上复制前一行的值?

[英]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沙田最后的FGrating,草
2017-09-03 2017-09-03 Sha Tin沙田 Grass 110 110 1736 1736
2017-09-16 2017-09-16 Sha Tin沙田 Grass 124 124 1736 1736 110 110
2017-10-14 2017-10-14 Sha Tin沙田 Grass 118 118 1736 1736 124 124
2017-11-11 2017-11-11 Sha Tin沙田 Grass 107 107 1736 1736 118 118
2018-03-28 2018-03-28 Happy Valley欢乐谷 Grass 117 117 1736 1736
2018-04-11 2018-04-11 Happy Valley欢乐谷 Grass 114 114 1736 1736
2018-09-22 2018-09-22 Sha Tin沙田 Grass 124 124 1736 1736 107 107

As you can see, there are spaces for the Last FGrating at Sha Tin, grass .如你所见,沙田的最后一次 FGrating,grass有空间。 These spaces have two causes:这些空间有两个原因:

  1. There is no last FGrating (the first record);没有最后的FGrating(第一条记录);
  2. There is a last FGrating for this track, but it was not written when the race for the same horse was on another track (Happy Valley grass, in this table).这条赛道有一个最后的 FGrating,但是当同一匹马的比赛在另一条赛道上时(欢乐谷草地,在这张表中),它没有写出来。

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:我正在寻找用以下值填充最右边一列的方法:

  1. 0, when no last FGrating exist (the first record, in this case) 0,当不存在最后一个 FGrating 时(在这种情况下是第一条记录)
  2. The last FGrating on Sha Tin grass on the records where the race was on other tracks, for the same horse.最后一次在沙田草地上的 FGrating 记录在其他赛道上,同一匹马。

The final table should look like this:决赛桌应如下所示:

Date日期 Track追踪 Surface表面 FGrating光栅 HorseId马号 Last FGrating at Sha Tin, grass沙田最后的FGrating,草
2017-09-03 2017-09-03 Sha Tin沙田 Grass 110 110 1736 1736 0 (no previous FGrating exists) 0(不存在之前的 FGrating)
2017-09-16 2017-09-16 Sha Tin沙田 Grass 124 124 1736 1736 110 110
2017-10-14 2017-10-14 Sha Tin沙田 Grass 118 118 1736 1736 124 124
2017-11-11 2017-11-11 Sha Tin沙田 Grass 107 107 1736 1736 118 118
2018-03-28 2018-03-28 Happy Valley欢乐谷 Grass 117 117 1736 1736 118 (the lastFGrating on Sha Tin grass at this point) 118(此时最后一次在沙田草地上打草)
2018-04-11 2018-04-11 Happy Valley欢乐谷 Grass 114 114 1736 1736 118 118
2018-09-22 2018-09-22 Sha Tin沙田 Grass 124 124 1736 1736 107 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)

Explanation解释

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.

相关问题 如何从熊猫数据框中获取同一行(上一列)的上一个值? - How to get the previous value of the same row (previous column) from the pandas dataframe? 如何获取 pandas 列中的前一行值 - how can I get a previous row value in pandas column 如何合并来自同一行和列索引/值的两个熊猫数据框的值? - How can I merge the values from two pandas dataframe which as same row and column indexes/value? 如何获取 Dataframe 中上一行的值以更改当前行值(动态地,而不是静态值) - How can I get the value of the previous row in a Dataframe to change the current row value (dynamically, not with a static value) 如何将数据框的值复制到另一个数据框的最后一列/行 - How to copy value of dataframe to another dataframe's last column/row 如何为数据框中的每一行分配一个值到不同的列? - How can I assign a value to a different column for each row in a dataframe? 如果只能在pandas数据框中使用它之后才能声明该列,则如何使用该列的前一行值进行计算? - How to calculate with previous row value of a column if it can be declared only after it should be used in pandas dataframe? Python Pandas Dataframe 根据同一列中的前一行值计算新行值 - Python Pandas Dataframe calculating new row value based on previous row value within same column 如何使用基于上一行和下一行的条件在 Pandas Dataframe 上创建新列? - How can I create a new column on a Pandas Dataframe with conditions based on previous and next row? 我如何根据每列的前一行创建一个新的 dataframe 单元格值? - How can i create a new dataframe with cell values based on the previous row for each column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM