pandas :计算均值而忽略自己行的值 [英] Pandas: Calculate mean leaving out own row's value

查看:61
本文介绍了 pandas :计算均值而忽略自己行的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按组计算均值,而忽略行本身的值.

I want to calculate means by group, leaving out the value of the row itself.

import pandas as pd

d = {'col1': ["a", "a", "b", "a", "b", "a"], 'col2': [0, 4, 3, -5, 3, 4]}
df = pd.DataFrame(data=d)

我知道如何按组返回均值:

I know how to return means by group:

df.groupby('col1').agg({'col2': 'mean'})

哪个返回:

Out[247]: 
  col1  col2
1    a     4
3    a    -5
5    a     4

但是我想要的是按组的意思,而忽略了行的值.例如.对于第一行:

But what I want is mean by group, leaving out the row's value. E.g. for the first row:

df.query('col1 == "a"')[1:4].mean()

返回:

Out[251]: 
col2    1.0
dtype: float64

预期的输出是与上述df格式相同的数据帧,其中的列mean_excl_own是该组中所有其他成员的平均值,不包括该行本身的值.

Expected output is a dataframe of the same format as df above, with a column mean_excl_own which is the mean across all other members in the group, excluding the row's own value.

推荐答案

您可以

You could GroupBy col1and transform with the mean. Then subtract the value from a given row from the mean:

df['col2'] = df.groupby('col1').col2.transform('mean').sub(df.col2)

这篇关于 pandas :计算均值而忽略自己行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆