ValueError - 数据函数

ValueError - DataFunction

当我 运行 我的代码时出现此错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

代码:

def calculate_pay(vehicle,hours):
    if vehicle == 'Bike':
        pay = 9.99
    elif vehicle == 'Walking & Public Transportation':
        pay = 9.99
    else:
        pay = 14.00
    return hours*pay

这是 excel 数据的示例:

我想要打印输出:

ID - Printed per row of data 

Vehicle Type used

Hours 

Total Pay calculated from the function 

这将导出回 Excel。

试试这个:

prices = {
    'Bike': 9.99,
    'Walking & Public Transportation': 9.99,
}
default_price = 14.00

df['TotalPay'] = df.apply(lambda x: prices[x.get('Vehicle', default_price)]*x['Hours'], axis=1)