【发布时间】:2021-12-14 16:51:40
【问题描述】:
运行代码时出现此错误:
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
我想打印输出:
ID - Printed per row of data
Vehicle Type used
Hours
Total Pay calculated from the function
这将被导出回 Excel。
【问题讨论】:
-
请说明
vehicle的定义以及您如何称呼calculate_pay... -
导出回excel是什么意思?另外请分享完整的代码,而不仅仅是功能。您需要打印函数中不存在的其他详细信息。我们会更容易理解。示例输出也会有所帮助。
-
你需要使用
df['Vehicle', 'Hours'].apply(calculate_pay) -
@user17242583 车辆将引用数据表中的 excel 列。我用这个来调用calculate_pay: df["Total Pay"] = df.apply(calculate_pay(df["Vehicle"], df["Hours"])) 我不确定这是否正确,因为我是新的所以请耐心等待,对不起。
-
你有这个想法,你只是在使用 Pandas 有点错误。我会写一个答案来帮助你。
标签: python pandas valueerror