0

I'm new to python. Some help would be great, thanks in advance too. I am trying to get the various average salary based on Units.

In Excel, I would do =AVERAGE(FILTER(B:B,A:A=D3)). Where Column B is all the individual salaries and Column A is the various Unit.

I have managed to do an array of the different units:

DiffUnits = df["Unit"].unique()

My data:

Units Salary
IT 500
Math 3000
Math 1200
Science 700
IT 2000
Science 1800

Expected result (In the form of a table, if possible):

Units AverageSalary
IT 1250
Math 2100
Science 1250
1
  • In Excel, you could have used AVERAGEIF/S. Commented Jul 30, 2022 at 17:08

1 Answer 1

2
import pandas as pd

df = pd.DataFrame({'Units': ['IT', 'Math', 'Math', 'Science', 'IT', 'Science'],
                   'Salary': [500, 3000, 1200, 700, 2000, 1800]})

df2 = df.groupby('Units').mean().reset_index()

df2

gives you:

     Units  Salary
0       IT    1250
1     Math    2100
2  Science    1250
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Didn't know 1 line would work already...
Use as_index=False argument of groupby and avoid reset_index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.