I have a pandas DataFrame with monthly product use by customers and country, like this:
df = pd.DataFrame(
[
('12345', 'CH', 'A', 'Prod 1'),
('12345', 'CH', 'A', 'Prod 2'),
('67890', 'DE', 'A', 'Prod 1'),
('98765', 'CH', 'B', 'Prod 3'),
('nnnnn', 'NL', 'C', 'Prod 1')
],
columns=['Client_ID', 'Country', 'Customer', 'Product Used']
)
I want to list the total amount of product use grouped by customer and country. The pandas groupby feature gets me close to what i need.
df.groupby(['Customer', 'Country','Product Used']).count()
#Reuse Client_ID as Count
Customer Country Product Used Client_ID
A CH Prod 1 3
Prod 2 5
DE Prod 1 1
B CH Prod 3 2
C NL Prod 1 1
Is there a way to include the combinations that don't appear in the data as 0? So my results look like this:
Customer Country Prod 1 Prod 2 Prod 3
A CH 3 5 0
DE 1 0 0
B CH 0 0 2
C NL 1 0 0