简体   繁体   English

表 pandas 中的条件均值

[英]Conditional mean in table pandas

df = {'Name': ['Gabriel', 'João', 'Marcela', 'Augusto', 'Mariana', 'Ana', 'Paula'],
      'Grupo Funcional':  ['Analista','Analista','Analista','Assessor','Diretor','Diretor','Gerente'],
      'Salary': ['1000', '1700', '1200', '600', '2000', '3000', '4000'],
        }

df = pd.DataFrame(df)

display (df)

What i need is the mean of 'Salary' by 'Grupo Funcional', something like that:我需要的是“Grupo Funcional”的“薪水”的意思,类似这样:

Mean Analista = R$ 3.500,00平均 Analista = R$ 3.500,00

Mean Diretor = R$ 2.500,00普通董事 = R$ 2.500,00

I know i can get this by.groupby, but this table has another columns like "Place of Work" and "Residence", so i would like a way to filter as many variables i want and get the mean of the Salary我知道我可以通过 groupby 得到这个,但是这个表还有其他列,比如“工作地点”和“住所”,所以我想要一种方法来过滤尽可能多的变量并获得工资的平均值

You can do that with something like the following您可以使用以下内容来做到这一点

df = {'Name': ['Gabriel', 'João', 'Marcela', 'Augusto', 'Mariana', 'Ana', 'Paula'],
      'Grupo Funcional':  ['Analista','Analista','Analista','Assessor','Diretor','Diretor','Gerente'],
      'Salary': [1000, 1700, 1200, 600, 2000, 3000, 4000],
        }

df = pd.DataFrame(df)

df2 = df.groupby("Grupo Funcional").agg({"Salary":"mean"}).reset_index()

print(df2)
  Grupo Funcional  Salary
0        Analista    1300
1        Assessor     600
2         Diretor    2500
3         Gerente    4000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM