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
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
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
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.