简体   繁体   中英

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:

Mean Analista = R$ 3.500,00

Mean Diretor = 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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