我有以下数据
amount = pd.Series(np.random.randn(10),name='amount')
df = pd.DataFrame({'amount': amount,'price':50})
df['month'] = ['jan','feb','mar','apr','apr','apr','may','may','jun','jun']
df.set_index('month')我想分组‘月’,和‘金额’栏,并显示价格旁边的每个月。我们可以假设价格在每个月内是一致的,因此使用一个mean()函数就可以了。
我不知道如何将sum()函数和sure ()函数组合在一起,并试图将两个函数分离,这似乎会导致信息丢失,例如:
df.groupby('month')[['amount']].sum()发布于 2018-07-18 21:59:25
在agg上使用groupby函数
df.groupby('month').agg({
'amount': sum,
'price': 'mean'
})这意味着您可以使用熊猫方法,方法是将其放入引号'mean'或numpy表示为np.mean。
https://stackoverflow.com/questions/51165370
复制相似问题