'Group by month and divide the average of one column by the average of the other?

I have two columns A and B. I want to divide the mean of A by the mean of B.

df['Test']= df.groupby('Month').apply(lambda x: df['A'].mean().astype(float)/df['B'].mean().astype(float))

Help me, where is the error?



Solution 1:[1]

DATA['C'] = DATA.apply(lambda x: np.array([x.A, x.B]), axis=1)

pandas requires all rows to be of the same length so the problem of uneven pandas series shouldn't be present

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 w_sz