Home > Back-end >  Conditional mean in table pandas
Conditional mean in table pandas

Time:02-14

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

CodePudding user response:

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
  • Related