用字典替换Python条件语句

如何通过在Python中应用字典来解决以下问题 想法是删除if并将其替换为字典并获得相同的结果。

def apply_fun(row,col='a'):
    if row[col] <= 5:
        val = 5
    elif row[col] <= 10:
        val = 10
    elif row[col] <= 15:
        val = 15
    elif row[col] <= 20:
        val = 20
    elif row[col] <= 25:
        val = 25        
    else:
        val = 30
    return val

values = [[12,2],[3,2],[23,2],[7,2],[19,2]]

db = pd.DataFrame(values, columns = ['a','b'])

db['new'] = db.apply(apply_fun, axis=1)

    a   b   new
0   12  2   15
1   3   2   5
2   23  2   25
3   7   2   10
4   19  2   20
评论