mardi 20 août 2019

Applying an If function to a dataframe

New to python so bare with me. 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)

Aucun commentaire:

Enregistrer un commentaire