I have 2 dataframes like this...
np.random.seed(0)
a = pd.DataFrame(np.random.randn(20,3))
b = pd.DataFrame(np.random.randint(1,5,size=(20,3)))
I'd like to find the average of values in a for the 4 groups in b.
This...
a[b==1].sum().sum() / a[b==1].count().sum()
...works for doing one group at a time, but I was wondering if anyone could think of a cleaner method.
My expected result is
1 -0.088715
2 -0.340043
3 -0.045596
4 0.582136
dtype: float64
Thanks.