Home > Mobile >  ValueError - DataFunction
ValueError - DataFunction

Time:10-30

I get this error when I run my code:

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

Here's the example of the excel data:

I'd like the output to print:

ID - Printed per row of data 

Vehicle Type used

Hours 

Total Pay calculated from the function 

This will be exported back into excel. Thanks for your help and sorry if this seems like a silly question, I'm new to Python :)

CodePudding user response:

Try this:

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)
  • Related