Skip to content
Advertisement

Pandas sum() with character condition

I have the following dataframe:

JavaScript

I want to use cumsum() in order to sum the values in column “1”, but only for specific variables: I want to sum all the variables that start with tt and all the variable that start with bb in my dataframe, so in the end i’ll have the folowing table :

JavaScript

I know how to use cumsum but I haven’t found any way to specify it to be only on specific rows that have something in common in one row (e.g the letters bb or tt).

Is there any way to use cumsum() in this way?

Advertisement

Answer

Use groupby,

JavaScript

Output

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