Multiple AggFun in Pandas

If you are thinking about pivot tables in Excel, you can add additional columns and go from sum to average or minimum. Is it possible to get multiple values ​​in pivotin Pandas?

Here is a working example (taken from pandas documentation):

import pandas as pd
import numpy as np
df = pd.DataFrame({'A' : ['one', 'one', 'two', 'three'] * 6,
....:              'B' : ['A', 'B', 'C'] * 8,
....:              'C' : ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 4,
....:              'D' : np.random.randn(24),
....:              'E' : np.random.randn(24),
....:              'F' : np.random.randn(24)})

Here is an example:

pd.pivot_table(df, values=['D', 'E'], rows=['B'], aggfunc=np.mean)

What returns:

    D           E
B       
A   -0.083449   -0.242955
B    0.826492   -0.058596
C    0.124266   -0.197583

Can I use an np.sumexample pivothere?

+1
source share
1 answer

You can pass a list to pivot_table aggfuncthe keyword argument:

>>> pd.pivot_table(df, values=['D', 'E'], rows=['B'], aggfunc=[np.mean, np.sum])
       mean                 sum          
          D         E         D         E
B                                        
A -0.102403  0.854174 -0.819224  6.833389
B  0.426928 -0.177344  3.415428 -1.418754
C -0.159123 -0.071418 -1.272980 -0.571341

[3 rows x 4 columns]

(PS: you can also use the version of the method, i.e df.pivot_table(stuff)..)

+5
source

Source: https://habr.com/ru/post/1541667/


All Articles