我正在用熊猫建立一些报告,我想与整体价值进行比较。
让我们说:
import pandas
data = {'puppy_name': ['Stanley', 'Doggo', 'Stanley', 'Doggo','Stanley', 'Doggo', 'Stanley', 'Doggo'],
'treats_earned': [25, 15, 20, 30, 20, 25, 20, 35],
'month': ['feb', 'feb', 'feb', 'feb','mar', 'mar', 'mar', 'mar']}
df = pandas.DataFrame(data)
我想做的是显示以下内容:
pup / month / pup_avg / overall_month_avg
Stanley | feb | 22.5 | 22.5
Doggo | feb | 20 | 22.5
Stanley | mar | 20 | 25
Doggo | mar | 27.5 | 25
因此,对于一列,我们按['month','pup']进行分组/含义,对于另一列,我们按['month']进行分组/含义。
答案 0 :(得分:1)
IIUC我们需要两次groupby
和map
s=df.groupby(['puppy_name','month'])['treats_earned'].mean().reset_index()
s['overall_month_avg']=s['month'].map(df.groupby('month')['treats_earned'].mean())
s
Out[33]:
puppy_name month treats_earned overall_month_avg
0 Doggo feb 22.5 22.5
1 Doggo mar 30.0 25.0
2 Stanley feb 22.5 22.5
3 Stanley mar 20.0 25.0