vendredi 26 juin 2020

How to use series. apply() to create conditional pandas series?

I am trying to create a new column in my df using numerical data from another column. I attempted using a for loop and a series of if statements to categorize the numerical data into strings that I want to now use to create the new column. The following data is from the WNBA 2010-2011 dataset about the players.

def clean(col):  
    for xp in col:
        if xp < 1:
            print('Rookie')
        elif ((xp >= 1) and (xp <= 3)):
            print('Little experience')
        elif ((xp >= 4) and (xp <= 5)):
            print('Experienced')
        elif ((xp > 5) and (xp < 10)):
            print('Very experienced')
        elif (xp > 10):
            print("Veteran")

I tried using series.apply() and series.map() but both of these return a new column called XP as follows

XP = df.Experience.apply(clean) 
df['XP'] = XP

However, when I checked the dtypes it says that the newly created column is a NONETYPE object. Is this because I am using the print function in the for loop as opposed to manipulating the actual value? If so what should I do to return the string values specified?

Thanks in advance for the help.

Aucun commentaire:

Enregistrer un commentaire