pandas breakdown of a column by other columns

Viewed 325

assuming I have the following dataframe:

    distributor   channel
 1   Warner        CH1
 2   Warner        CH2
 3   Warner        CH2
 4   Warner        CH3
 5  Columbia       CH4

what I want is to get the distribution of channels per distributor, in this simple example:

    distributor  CH1 CH2 CH3 CH4 
1    Warner      25% 50% 25%  0%
2   Columbia      0% 0%  0%  100%

I looked into the density function and other similar functions but couldn't figure it out.

Any help will be appreciated!

1 Answers

Using crosstab with normalize

pd.crosstab(df.distributor,df.channel,normalize='index')
Out[506]: 
channel       CH1  CH2   CH3  CH4
distributor                      
Columbia     0.00  0.0  0.00  1.0
Warner       0.25  0.5  0.25  0.0
Related