Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Take first observation per group Using pandas.pivot_table

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()?

like image 768
Zhongjie Zhang Avatar asked Feb 06 '15 07:02

Zhongjie Zhang


1 Answers

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...

like image 160
Andy Hayden Avatar answered Sep 29 '22 06:09

Andy Hayden