0

I have a pandas which is 6x3 and the columns represent times.

I want to replace values on conditions:

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)

where c and k are constant. And n is the column name. It gives me the error

"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')"

I tried to look at replace function but they did not work.

Thegamer23
  • 537
  • 1
  • 8
  • 20

1 Answers1

0

Here I have simplified your code.

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)

Modifications:

  1. applymap to replace element-wise.
  2. Function names were inconsistent: substituteMin, compareMin.
  3. In the function (substituteMin), constant variables (n,c,k) can be set in the function only.

(There is plenty to comment and edit in your question. Currently, I don't have privileges.)

Community
  • 1
  • 1
  • Thanks but how can I pass n to be the column name? I would do "lambda x: func(x, x.name)" but it does not work – Thegamer23 May 15 '17 at 07:25
  • that's because `x` would not have have an attribute `name`. to accomplish your objective I guess you should simply iterate over rows and columns (nested). eg. `for col in df: for row in df.index: y=func(df.loc[row,col],col)` Note: go to new line after `:`s. it's not a one liner like `lambda`. –  May 15 '17 at 13:58