0

I have the following dataset:

Year Company Product Sales
2017  X       A       10
2017  Y       A       20
2017  Z       B       20 
2017  X       B       10
2018  X       B       20
2018  Y       B       30
2018  X       A       10 
2018  Z       A       10

I want to obtain the following summary:

Year Product Sales
2017 A       30
     B       30
2018 A       50 
     B       20

and also the following summary:

Year Company Sales
2017  X      20
      Y      20 
      Z      20
2018  X      50
      Y      10 
      Z      10

Is there any way to do it without using loops?

I know I could do something with the function aggregate, but I don't know how to proceed with it without mixing the data of company, product and year. For example, I get the total sales of product A and B, but it's mixing the sales of both years instead of giving A and B in 2017, and separated in 2018.

Do you have any suggestions?

karel
  • 5,489
  • 46
  • 45
  • 50

1 Answers1

0

Let's say your dataframe is called df:

df1 = df.groupby('Year', 'Product')['Sales'].sum()
df2 = df.groupby('Year', 'Company')['Sales'].sum()

I believe this would help you create your two summary dataframes without mixing anything :) !

helloworld
  • 416
  • 2
  • 6
  • 15