Skip to content
Advertisement

Pandas – Adding individual Z-Scores to each row based on an id

So I have a Pandas data-frame with a game_id, player_id, and playtime column. I would like to add a z-score rating for each row to find how much from the norm, in terms of playtime, they are for each given game. How would I go through and add each one of these scores to a new column for the data-frame? Let me know if there’s anything I need to clarify.

Advertisement

Answer

The simplest way and more efficient (fastest) is using the function apply of the data frame. First define the function of how you calculate the new column, and then apply it with the apply function:

def creating_new_column(row):
    #Adapt your code for the way you calculate the z score
    return ((row["playtime "])/2)

df["z_score"]=df.apply(lambda row: creating_new_column (row), axis=1)
Advertisement