2

Hello a Python newbie here.

I have a dataframe that shows the product and how much they sold on each date enter image description here

I need to change this dataframe to show the aggregate amount of units sold. enter image description here

This is just an example dataframe and the actual dataframe that I am dealing with contains hundreds of products and 3 years worth of sales data.

It would be appreciated if there would be a way to do this efficiently.

Thank you in advance!!

Adam Lee
  • 99
  • 1
  • 4
  • 13

1 Answers1

1

If product is column use DataFrame.set_index with DataFrame.cumsum for cumulative sum:

df1 = df.set_index('product').cumsum(axis=1)

If product is index:

df1 = df.cumsum(axis=1)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    @AdamLee Make sure to check that your columns are in chronological order, and sort them if they aren't (`.sort_index(axis=1)`). – Swier Apr 22 '21 at 08:31