I have:
df = pd.DataFrame({'A':[1, 2, -3],'B':[1,2,6]})
df
A B
0 1 1
1 2 2
2 -3 6
Q: How do I get:
A
0 1
1 2
2 1.5
using groupby()
and aggregate()
?
Something like,
df.groupby([0,1], axis=1).aggregate('mean')
So basically groupby along axis=1
and use row indexes 0
and 1
for grouping. (without using Transpose)
df.apply(pd.Series.mean, 1)
? You can also get a dataframe out of this withdf.apply(pd.Series.mean, 1).to_frame('A')
. – Combo