0

I am trying to apply an IF function to a column in a dataframe. Quite simple, I want it to return 'Over £50,000' if the salary is over £50,000 and 'Under £50,000' if it is under.

I have tried the code below but I am met with the error message. I have done some research into using the apply function when working with a dataframe, so defined a function and used the second piece of code below, but got the second error message.

    salary = employees['Salary']

    if Salary > 50000:
        print('Over £50,000')
    else:
        print('Under £50,000')

Apply Method

    def SalaryCheck():
        if Salary > 50000:
            print('Over £50,000')
        else:
            print('Under £50,000') 

    employees.Salary.apply(SalaryCheck)


    Traceback (most recent call last):
      File "<input>", line 1, in <module>
      File "C:\Users\Peter\PycharmProjects\untitled\venv\lib\site-                        packages\pandas\core\generic.py", line 1556, in __nonzero__
        self.__class__.__name__
    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Apply Method Warning

    Traceback (most recent call last):
      File "<input>", line 1, in <module>
      File "C:\Users\Peter\PycharmProjects\untitled\venv\lib\site-        packages\pandas\core\series.py", line 4038, in apply
        mapped = lib.map_infer(values, f, convert=convert_dtype)
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 1
    `def SalaryCheck(x)` then check `if x > 50000`, though this can be done without the apply using `np.where`. See https://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column – ALollz Aug 20 '19 at 15:20
  • Hi Peter, I'm pretty sure your problem is caused by the fact that your `SalaryCheck` function doesn't have any parameters defined for it, so the line `if Salary > 50000` looks to actually refer to every single salary in your dataframe, rather than any one particular salary. Gokul's answer should help you. That said, this question is caused by a simple typographical problem, so is likely to be closed as off-topic for this site. – ymbirtt Aug 20 '19 at 15:27
  • Peter, in your first example where is upper case `Salary` defined? As for the second, this looks like a better explanation of the error message, worth understanding to know why pandas was unhappy: https://stackoverflow.com/a/45494393/5590742 (edit: replaces older comment replaced with more clear link). – jgreve Aug 20 '19 at 15:49

2 Answers2

1

Another approach:

(employee['salary'] > 50000).replace({True: 'Over', False: 'Under'})

employee['salary'] > 50000 returns a Series of True / False, then you simply replace True and False with the text you want.

Code Different
  • 90,614
  • 16
  • 144
  • 163
0

You can use the following approach. Please read about apply() for clarification.

def SalaryCheck(Salary):
    if Salary > 50000:
        print('Over £50,000')
    else:
        print('Under £50,000') 

lst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']
lst2 = [110000, 220000, 3300, 440000, 55, 66, 77000]


df = pd.DataFrame(list(zip(lst, lst2)),
                  columns=['Name', 'Salary']) 
df['Salary'].apply(SalaryCheck)[0]