I need to achieve several things:
Group columns by Country and Products
Perform aggregation to get:
- percentage of my Products column for each country - Calculate the sum of columns Volume and Profit and UnrealizedProfit (2 columns 1st=Volume, 2nd= Profit + UnrealizedProfit)
Display other columns as well
My DataFrame:
Country Sector Products Volume Profit UnrealizedProfit
0 Country_1 Sector1 Product_1 50 5 4
1 Country_1 Sector2 Product_2 100 6 3
2 Country_2 Sector1 Product_1 150 3 -1
3 Country_2 Sector2 Product_2 200 -1 5
4 Country_1 Sector1 Product_2 100 7 10
5 Country_2 Sector2 Product_2 200 -3 -1
6 Country_2 Sector1 Product_1 150 2 -1
7 Country_1 Sector2 Product_1 50 5 -3
Note: I have a couple out thousand rows in my actual dataframe.
Desired output would look like this:
Country Sector Products Product% Volume ExpectedProfit
0 Country_1 Sector1 Product_1 0.138 100 11
1 Country_1 Sector2 Product_2 0.861 200 26
2 Country_2 Sector1 Product_1 0.667 300 3
3 Country_2 Sector2 Product_2 0.333 400 0
I only managed to do one aggregation at a time but not two. So far:
df = (data1.groupby('Country')['Products']
.value_counts(normalize=True,sort=False)
.reset_index(name='Product%'))
print (df)
This code gives me:
Country Products Product%
0 Country 1 Product 1 0.138
1 Country 1 Product 2 0.861
2 Country 2 Product 1 0.667
3 Country 2 Product 2 0.333
Each frequency of product is based on the related country --> sum(Country1) =100%, sum(Country2)=100%...
And for the Volume I manage to replicate it:
df = (data1.groupby(['Country','Product'])['Volume']
.sum()
.reset_index(name='Volume'))
I added the Product in the groupby() because I want to see the volume for each product and country.
The Goal is to combine the Products% and Volume and adding the ExpectedProfit I don't know how to combine that and to do the aggregation for the profit, as mentioned (Profit+UnrealizedProfit) And as well displaying the Sector (I guess the sector could be included in the Groupby() since each the Sector has several products.
Thank you for your help!