Pandas

Pandas (26)

Jun's N 2022. 8. 17. 19:53

 How to get the mean of a series grouped by another series?

# Input
fruit = pd.Series(np.random.choice(['apple', 'banana', 'carrot'], 10))
weights = pd.Series(np.linspace(1, 10, 10))

# Solution
weights.groupby(fruit).mean()

# output
apple     7.4
banana    2.0
carrot    6.0
dtype: float64
728x90