Reputation: 2220
I have a data frame which contains the information of customer type. I used groupby
to count the type of customer within each category using following command:
df.groupby('Classification')['customer'].count()
It produces following result:
Classification
Agriculture 2366
Commercial 21904
Council 414
Industrial 911
Residential 51018
Name: ICP, dtype: int64
Now I want to calculate the percentage of each type of classification. I used follwoing command to calculate:
df.groupby(level=0).apply(lambda x: 100 * x/float(x.sum()))
But it produces following output:
Classification
Agriculture 100.0
Commercial 100.0
Council 100.0
Industrial 100.0
Residential 100.0
Name: ICP, dtype: float64
I want to show the percentage of each type of classification. Not sure where am I making the mistake. Any help would be really appreciated.
Upvotes: 0
Views: 166
Reputation: 10624
The following should work:
df.groupby('Classification')['customer'].count()/df['customer'].count()
Upvotes: 2