I am posting this because the topic just grew up in another question / answer and the behavior is not very well documented.
Consider a df data block
df = pd.DataFrame(dict( A=list('xxxyyy'), B=[np.nan, 1, 2, 3, 4, np.nan] )) AB 0 x NaN 1 x 1.0 2 x 2.0 3 y 3.0 4 y 4.0 5 y NaN
I wanted to get the first and last rows of each group, defined by column 'A' .
I tried
df.groupby('A').B.agg(['first', 'last']) first last A x 1.0 2.0 y 3.0 4.0
However, this does not give me the np.NaN that I was expecting.
How to get the actual first and last values ββin each group?
source share