python-3.x - 应用通用函数 (x,y) 从两个现有列创建一个新列,以便我可以在不同列中使用该函数

标签 python-3.x pandas function dataframe apply

我正在尝试计算要应用于数据帧的两列的每一行的折扣,并将结果添加到新列中。

我已经尝试了很多方法,按照现有的例子,但每次都会出现错误。

我将函数定义为:

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/

相关文章:

Python 3 - 用下一个字母检查字符串中的字母

python Pandas : How do I drop specific levels in a hierarchical index if any column values are NaN?

r - 在函数内部使用 dplyr 时出错

javascript - 检查javascript中是否存在函数

python - 我想用列表中第一次出现的字符串替换该字符串

python - 为什么 "type"不在 "int"的 MRO 中?

python - 从函数返回 str 和 int

python - 如何复制数据框中的条目

python - 有没有好的方法可以将 pandas 列多索引转换为列类别?

function - Delphi函数通用