Skip to content
Advertisement

How can I use cumsum skipping the first entry?

I have a DF that contains the ids of several creators of certain projects and the outcomes of their projects over time. Each project can either be a success (outcome = 1) or a failure (outcome=0). The DF looks like this:

JavaScript

I’m looking for a way to create two new columns: previous projects and previous successes.

The first should be the cumulative number of previous projects for any creator. Given that I’ve already ordered the DF by creator and by date of creation, I know I can do this by running the following code:

JavaScript

My issue is that I can’t figure out how to create the column previous success. I tried to run the following code:

JavaScript

The issue with this code is that it starts counting immediately (the success of row x is counted as previous success for the project in row x). Moreover, I have issues with projects whose outcome path is not homogeneous, i.e. one creator’s sequence is: success, failure, success.

Ideally my DF should look like:

JavaScript

Please note the peculiar sequence required as result for creator_id number 2. Hope all is clear

Advertisement

Answer

You can use shift with fillna to start your cumsum from 0:

JavaScript

prints

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement