【发布时间】:2020-09-02 05:56:37
【问题描述】:
我正在尝试使用特定子集的前一行中的值(满足条件时)填充新列(上一时间)中的每一行。问题是,如果我中断内核并检查值,就可以了。但如果它运行到最后,则新列中的所有行都将填充为 None。如果上一行不存在,那么我将用第一个值填充它。
Name First round Previous time
Runner 1 2 2
Runner 2 5 5
Runner 3 5 5
Runner 1 6 2
Runner 2 8 5
Runner 3 4 5
Runner 1 2 6
Runner 2 5 8
Runner 3 5 4
我尝试了什么:
df.insert(column = "Previous time", value = 999)
def fce(arg):
runner= arg[0]
stat = arg[1]
if stat == 999:
# I used this to avoid filling all rows in a new column again for the same runner
first = df.loc[df['Name'] == runner,"First round"].iloc[0]
df.loc[df['Name'] == runner,"Previous time"] = df.loc[df['Name'] == runner]["First round"].shift(1, fill_value = first)
df["Previous time"] = df[['Name', "Previous time"]].apply(fce, axis=1)
【问题讨论】:
标签: pandas dataframe apply nonetype