Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate maximum from pivot table in python

Tags:

python

pandas

I have written code to calculate the count of feature3 groupby feature1 and feature2

pd.pivot_table(data=train, index=['feature1', 'feature2'], values=['feature3'], aggfunc='count')

Who's output is:

feature1 feature2 feature3  
129001  0   4
        1   10
        2   11
        3   22
        4   26
        5   38
129002  0   6
        2   45
        5   25

Now I want to calculate max of feature3 groupby feature1

feature1 feature3
129001    38
129002    45
like image 737
Rajeev Kumar Sahu Avatar asked Feb 01 '26 21:02

Rajeev Kumar Sahu


1 Answers

IIUC:

You need the following statement:

df.groupby(level=0)['feature3'].max()

Start with the results of your pivot_table

print(df)
                   feature3
feature1 feature2          
129001   0                4
         1               10
         2               11
         3               22
         4               26
         5               38
129002   0                6
         2               45
         5               25

groupby with level 0 of your index and max:

df.groupby(level=0)['feature3'].max()

Output:

feature1
129001    38
129002    45
Name: feature3, dtype: int64
like image 139
Scott Boston Avatar answered Feb 03 '26 11:02

Scott Boston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!