Pandas 使用 Lambda 例子及注意事项

132 阅读1分钟
# 注意 函数 formula() 一定要有返回值,否则下面 apply 函数不起作用
def formula(x):
    if x in slidetime_dict:
        return slidetime_dict[x]
    else:
        return 15
 
df['slidetime'] = df.apply(lambda row: formula(row['A']), axis=1)

注意:

pandas 使用 apply() 函数时,被调用的函数(本例中是 formula())中各种条件分支都要有 return 返回值,否则生成的结果不对。