How can I specify a function in the aggfunc=[] using pandas.pivot_table so that I get the first observation of every group just like the result I get running groupby().first()?
You can use aggfunc='first':
In [11]: df = pd.DataFrame([[1, 2, "A"], [1, 4, "A"], [5, 6, "B"]])
In [12]: df
Out[12]:
0 1 2
0 1 2 A
1 1 4 A
2 5 6 B
In [13]: df.pivot_table(index=0, values=1, columns=2) # default aggfunc is 'mean'
Out[13]:
2 A B
0
1 3 NaN
5 NaN 6
In [14]: df.pivot_table(index=0, values=1, columns=2, aggfunc='first')
Out[14]:
2 A B
0
1 2 NaN
5 NaN 6
I'm not sure if there's a complete list of these strings for aggfunctions in the docs (they also work for groupbys), I will take a look...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With