1
def function(df):
    if (df['Total'] > 0) & (df['Total'] <= 50000):
         df['X'] = (df['Total']*(2/150)) * 0.2
    elif (df['Total'] > 50000):
        df['X'] = ((50000*(2/150))*0.2) + ((df['Total']-50000)*(2/150)*0.2)

I am trying to run the above code but getting a value error saying

File "<ipython-input-31-f77514d81c6f>", line 1, in <module>
    platinumplus(cust_spends)

File "<ipython-input-30-da2fd213761f>", line 2, in platinumplus
    if (df['Total'] > 0) & (df['Total'] <= 50000):

File "E:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", 
    line 1573, in __nonzero__ .format(self.__class__.__name__))

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Abhay kumar
  • 203
  • 1
  • 4
  • 13

2 Answers2

3

I think the issue is in your if statements. (df['Total'] > 0) & (df['Total'] <= 50000) will return a boolean series rather than a single True or False so python doesn't know how to handle this. If you want the case where all of the values in the series are True you can use:

((df['Total'] > 0) & (df['Total'] <= 50000)).all()

Sven Harris
  • 2,884
  • 1
  • 10
  • 20
  • 1
    it partially worked, 2nd loop is not working. – Abhay kumar Oct 22 '18 at 06:26
  • 1
    So I think this answer addresses the source of the error (you would have to use the same logic on your second if statement). However it looks like this might not be doing what you expect - setting values based on how they evaluate in the series for which I suggest you look at the solution using `numpy.where` – Sven Harris Oct 22 '18 at 06:35
2

I believe you need numpy.where for set new values of column by boolean mask:

df = pd.DataFrame({'Total':[10, 10000, 40000, 100]})

def function(df):
    mask = (df['Total'] > 0) & (df['Total'] <= 50000)
    v1 = (df['Total']*(2/150)) * 0.2
    v2 = ((50000*(2/150))*0.2) + ((df['Total']-50000)*(2/150)*0.2)

    df['X'] = np.where(mask, v1, v2)
    return df

df1 = df.pipe(function)
print (df1)
   Total           X
0     10    0.026667
1  10000   26.666667
2  40000  106.666667
3    100    0.266667

If there is multiple conditions use numpy.select:

df = pd.DataFrame({'Total':[10, 10000, 40000, 100, -2]})


def function(df):
    mask1 = (df['Total'] > 0) & (df['Total'] <= 50000)
    mask2 = df['Total'] > 50000
    v1 = (df['Total']*(2/150)) * 0.2
    v2 = ((50000*(2/150))*0.2) + ((df['Total']-50000)*(2/150)*0.2)

    df['X'] = np.select([mask1, mask2], [v1, v2], default=np.nan)
    return df

df1 = df.pipe(function)
print (df1)
   Total           X
0     10    0.026667
1  10000   26.666667
2  40000  106.666667
3    100    0.266667
4     -2         NaN
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252