【发布时间】:2021-10-08 14:13:45
【问题描述】:
我遇到了一个问题,我认为应该是直截了当的。 问题是我有一个函数,我想将它应用于我的数据框的两列。但我收到一个错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
向你展示我想要做什么:
# Calculate the accuracy
def mape(actual,pred):
if actual == 0:
if pred == 0:
return 0
else:
return 100
else:
return np.mean(np.abs((actual - pred) / actual)) * 100
然后,我尝试将其应用于两列(称为 Actuals_March 和 Forecast_March)。
# This line runs into the ValueError above.
# I removed all NaN values before running this.
df['MAPE_Mar'] = df.apply(lambda x: mape(df.Actuals_March , df.Forecast_March), axis=1)
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
#This is an snapshot of my data:
df.Actuals_March df.Forecast_March
0.0 0.0
0.0 0.0
0.0 0.0
4.0 0.0
0.0 0.0
5.0 0.0
20.0 0.0
0.0 0.0
2.0 0.0
13.0 0.0
希望你能帮助我。提前致谢
【问题讨论】:
标签: python pandas function apply