我正在尝试计算要应用于数据帧的两列的每一行的折扣,并将结果添加到新列中。
我已经尝试了很多方法,按照现有的例子,但每次都会出现错误。
我将函数定义为:
def delta_perc(x,y):
if y == 0:
return 0
else:
return (x-y)/x*100
然后尝试将该函数应用于我的数据框
ordini["discount"] = ordini.apply(delta_perc(ordini["revenue1"],ordini["revenue2"]), axis=1)
我期望一个新列,其中每一行都是应用于 ordini["revenue1"] 和 ordini["revenue2"] 的函数的结果。
但我收到以下错误:
ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
我还尝试应用 here 的所有建议但每次都会出现错误。
最佳答案
您混淆了一些概念。当您使用pandas.DataFrame.apply(使用axis=1)时,您将迭代每一行并传递该行(作为pandas.Series) > object) 到您调用 apply
时使用的函数。
第一个失败点
相反,您在 apply
中调用函数并将两列传递给该函数。这会将函数的返回值传递给apply
。由于您的函数不会传回可调用对象,因此这应该会失败。
第二个故障点
此外,您的函数设计为查看标量值,因此 if == 0:
以及当您传递像 ordini["revenue1"]
这样的列(即一个 pandas.Series
对象),它尝试评估 if pandas.Series == 0:
这就是生成您看到的错误的原因:
<小时/>ValueError: The truth value of a Series is ambiguous.
方法#1
修复你的函数并且不要使用apply
def delta_perc(x, y):
return x.sub(y).div(x).mask(x == 0, 0).mul(100)
ordini["discount"] = delta_perc(ordini["revenue1"], ordini["revenue2"])
<小时/>
方法#2
修复你的功能并使用map
。这类似于使用推导式。
def delta_perc(x, y):
if x == 0:
return 0
else:
return (x - y) / x * 100
ordini["discount"] = [*map(delta_perc, ordini["revenue1"], ordini["revenue2"])]
<小时/>
方法#3
实际上使用apply
def delta_perc(x, y):
if x == 0:
return 0
else:
return (x - y) / x * 100
# Because remember `apply` takes a function that gets a row (or column) passed to it
ordini["discount"] = ordini.apply(
lambda row: delta_perc(row['revenue1'], row['revenue2']),
axis=1
)
关于python-3.x - 应用通用函数 (x,y) 从两个现有列创建一个新列,以便我可以在不同列中使用该函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55692124/