0

My Python Pandas data frame has 2 columns for salary(Amount) and total number of employees(Staff) receiving that particular salary (10 employees get $300, 20 employees get $200 & 30 employees get $100).I'm supposed to calculate the average salary of all employees. Is there any way to do that? I'm fairly new to Python Pandas so any help would be appreciated! Thanks in advance.

   Amount  Staff
0     100     30
1     200     20
2     300     10  

2 Answers 2

1

It's exactly as you described - no special techniques.

df = pd.read_csv(io.StringIO("""   Amount  Staff
0     100     30
1     200     20
2     300     10"""), sep="\s+")

(df["Amount"] * df["Staff"]).sum() / df["Staff"].sum()

output

166.66666666666666
Sign up to request clarification or add additional context in comments.

Comments

0

I think you can use mean by defintion - sum / counts:

s = df.assign(Amount = df["Amount"].mul(df["Staff"])).sum()
print (s)
Amount    10000
Staff        60
dtype: int64

print (s.Amount / s.Staff)
166.66666666666666

Comments

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.