Skip to content Skip to sidebar Skip to footer

Python Pandas: Cumulative Sum Based On Multiple Conditions

I am calculating the value for the Total ‘1st’ Position column (table below) and would like to do this using multiple conditions. I want Total ‘1st’ Position to reflect the

Solution 1:

You can do it this way:

df = your_file

df.loc[(df['Position'] == 1), 'firsts'] = 1
df=df.fillna(0)

df['Total 1st Position'] = (df['firsts']*df['Position']).groupby(df['Athlete']).cumsum()

If we run your data frame through this we get the following:

   Race Day Athlete  Position  firsts  Total 1st Position
0Day1   Steve         11.01.01Day1    Jane         20.00.02Day1    Bill         30.00.03Day2    Bill         11.01.04Day2   Steve         20.01.05Day2    Jane         30.00.06Day3    Jane         11.01.07Day3    Bill         20.01.08Day3   Steve         30.01.09Day4   Steve         11.02.010Day4    Jane         20.01.011Day4    Bill         30.01.0

Solution 2:

Create a temporary column to indicate wins, then use .groupby with .cumsum on that:

df['won'] = (df['Position'] == '1') * 1
df['Total 1st Position'] = df.groupby('Athlete').won.cumsum()

Post a Comment for "Python Pandas: Cumulative Sum Based On Multiple Conditions"