Adam Lee
Adam Lee

Reputation: 97

Making Pandas dataframe to display aggregate values based on date

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!!

Upvotes: 2

Views: 50

Answers (1)

jezrael
jezrael

Reputation: 862661

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)

Upvotes: 1

Related Questions