'Pandas pivot table. Getting modes and relevant percentage

Here is the problem. While creating a pivot table in pandas data frame, I need to aggregate the column values for their modes as well as their relevant percentages.

This piece of code gives me only a mode of 'C' column, but I need both a mode and its percentage share.

df.pivot_table(index = 'A', values = 'C', aggfunc = lambda x: x.mode())


Solution 1:[1]

A simple way is to create percentile functions:

import numpy as np 
def per_25(df):
        return np.percentile(df, 25)
    def per_50(df):
        return np.percentile(df, 50) # 50th percentile is the mode
    def per_75(df):
        return np.percentile(df, 75)

and then input functions in aggfunc:

df.pivot_table(index = 'A', values = 'C', aggfunc = [per_25, per_50, per_75]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ramon