2 回答
TA贡献1847条经验 获得超7个赞
第一个想法是替换0
为NaN
s 然后使用GroupBy.transform
with mean
,默认情况下省略缺失值:
print (df.assign(new = df['Cost'].where(df['Cost'] > 0)))
Day Country Type Product Cost new
0 Mon US 1 a1 0 NaN
1 Mon US 2 a1 5 5.0
2 Mon US 3 a1 6 6.0
3 Mon CA 1 a1 8 8.0
4 Mon CA 2 a1 0 NaN
5 Mon CA 3 a1 1 1.0
df['Average'] = (df.assign(new = df['Cost'].where(df['Cost'] > 0))
.groupby(['Country','Product'])['new']
.transform('mean'))
print (df)
Day Country Type Product Cost Average
0 Mon US 1 a1 0 5.5
1 Mon US 2 a1 5 5.5
2 Mon US 3 a1 6 5.5
3 Mon CA 1 a1 8 4.5
4 Mon CA 2 a1 0 4.5
5 Mon CA 3 a1 1 4.5
或者首先过滤、聚合mean并按以下方式分配回来DataFrame.join:
s = df[df["Cost"] > 0].groupby(['Country','Product'])['Cost'].mean().rename('Average')
df = df.join(s, on=['Country','Product'])
print (df)
Day Country Type Product Cost Average
0 Mon US 1 a1 0 5.5
1 Mon US 2 a1 5 5.5
2 Mon US 3 a1 6 5.5
3 Mon CA 1 a1 8 4.5
4 Mon CA 2 a1 0 4.5
5 Mon CA 3 a1 1 4.5
TA贡献1828条经验 获得超4个赞
尝试这个 :
df[df["Cost"] > 0].groupby(['Country','Product'])["Cost"].mean()
它过滤掉成本大于零的地方,对其进行分组,然后取平均值。
添加回答
举报