How to calculate maximum from a pivot table in python

I wrote a code to count the number of features feature3 groupby feature1 and feature2

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

      

Who issues:

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 the maximum of feature3 groupby feature1

feature1 feature3
129001    38
129002    45

      

+3


source to share


2 answers


IIUC:

You will need the following instructions:

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

from level 0

your index and max

:

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

      

Output:

feature1
129001    38
129002    45
Name: feature3, dtype: int64

      

+2


source


You can do it like this:



In [21]: df
Out[21]:
                   feature3
feature1 feature2
129001   0                4
         1               10
         2               11
         3               22
         4               26
         5               38
129002   0                6
         2               45
         5               25

In [22]: df.max(level='feature1')
Out[22]:
          feature3
feature1
129001          38
129002          45

      

+2


source







All Articles