Consider the dataframe df
df = pd.DataFrame(dict(
A=list('xxxyyy'),
B=[np.nan, 1, 2, 3, 4, np.nan]
))
df
A B
0 x NaN
1 x 1.0
2 x 2.0
3 y 3.0
4 y 4.0
5 y NaN
I can use a function within an agg
and pass an argument like this
df.groupby('A').B.agg(pd.Series.head, n=1)
A
x NaN
y 3.0
Name: B, dtype: float64
However, I want to run the aggregation with pd.Series.head
and pd.Series.tail
. And I want to pass the argument n=1
to both of them.
I want this aggregation to look like the result below. It is important to note that I can produce this result already. My goal here is to figure out how to pass arguments to the multiple functions that are being passed to agg
.
If it can't be done, an explanation why would be a valid answer.
h t
A
x NaN 2.0
y 3.0 NaN
Added Incentive
If you figure this out... it would be a better solution than the one I have for this question. I would encourage whoever answers this one to also answer that one.