对数据集应用函数以动态替换

2024-10-03 15:22:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一只熊猫,它是6x3,列代表时间。你知道吗

我要在以下条件下替换值:

def substituteMin(x,n,c,k):
    if x < (1 - c)^n+sqrt(k):
        x = (1 - c)^n+sqrt(k)
    else:
        pass
    return x
df1 = df.apply(lambda x: compareMin(x, x.name))
print (df1)

其中c和k是常数。n是列名。 它给了我一个错误

"ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', u'occurred at index 1.0')"

我试图看看替换功能,但他们没有工作。你知道吗


Tags: lambdadfreturnifdef时间代表pass
1条回答
网友
1楼 · 发布于 2024-10-03 15:22:45

这里我简化了你的代码。你知道吗

def func(x,n=0,c=1,k=2):
    if x < (1 - c)^n+sqrt(k):
        x = (1 - c)^n+sqrt(k)
    return x
df1 = df.applymap(lambda x: func(x))
print (df1)

修改:

  1. ^{}替换元素。你知道吗
  2. 函数名不一致:substituteMin、compareMin。你知道吗
  3. 在函数(substituteMin)中,常量变量(n,c,k)只能在函数中设置。你知道吗

(你的问题中有很多可以评论和编辑的地方。目前,我没有特权。)

相关问题 更多 >