Search code examples
pythonpandascumsum

How to combine cumulative sum with data in Pandas


For instance, here is the dataframe that I have.

import pandas as pd 
data = [[15, 10, 20], [1, 15, 6], [8, 14, 26]]
di = pd.DataFrame(data, columns = ['Day 1', 'Day 2', 'Day 3'])

This leads to the following dataframe.

di = pd.DataFrame( {'Day 1': {0: 15, 1: 1, 2: 8}, 'Day 2': {0: 10, 1: 15, 2: 14}, 'Day 3': {0: 20, 1: 6, 2: 26}} )

How do I transform the above dataframe such that each cell within the pandas dataframe contains the value and the cumulative sum (in brackets), i.e.

di_transformed = pd.DataFrame( {'Day 1': {0: 15 (15), 1: 1 (1), 2: 8 (8)}, 'Day 2': {0: 10 (25), 1: 15 (16), 2: 14 (22)}, 'Day 3': {0: 20 (45), 1: 6 (22), 2: 26 (48)}} )


Solution

  • You can just concatenate the strings:

    di.astype(str) + ' (' + di.cumsum(axis=1).astype(str) + ')'
    

    output:

         Day 1    Day 2    Day 3
    0  15 (15)  10 (25)  20 (45)
    1    1 (1)  15 (16)   6 (22)
    2    8 (8)  14 (22)  26 (48)