2
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().

8
  • 1
    Can you show us the stack trace?
    – blhsing
    Commented Oct 22, 2018 at 5:56
  • Can you please show the data set? Commented Oct 22, 2018 at 6:02
  • Please, chech that df['Total'] type is float. It might be string
    – jalazbe
    Commented Oct 22, 2018 at 6:02
  • 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(). Commented Oct 22, 2018 at 6:02
  • @Abhaykumar Then can you please post it on the question, edit it then add it? Commented Oct 22, 2018 at 6:03

2 Answers 2

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()

2
  • 1
    it partially worked, 2nd loop is not working. Commented Oct 22, 2018 at 6: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 Commented Oct 22, 2018 at 6: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
3
  • what if there is one more elif condition? Commented Oct 22, 2018 at 6:27
  • @Abhaykumar - added solution. If new elif condition create new boolean mask and new values mask3 with v3.
    – jezrael
    Commented Oct 22, 2018 at 6:29
  • 1
    @jpp - I agree, thank you.
    – jezrael
    Commented Oct 22, 2018 at 10:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.