Sandeep
Sandeep

Reputation: 1247

How to get mean of "specific" column value within a group by in pandas

I have a simple test code as below and I want to know the average salaries of each department. Specifically "sales", but when I use groupby on "Dept" and do a mean I get the mean of all the departments.

df = pd.DataFrame({"Dept":["sales", "engg", "mkt", "sales", "engg","mkt", "sales", "sales", "engg", "mkt"],"Salaries": [10,5,20,15,60,25,35,40,10,20]})
df.groupby("Dept")["Salaries"].mean()

Dept
engg     25.000000
mkt      21.666667
sales    25.000000
Name: Salaries, dtype: float64

However if I want to get "sales" alone average, I do the below.I want to know is there a better way to do the same thing using groupby and pull out average of specific column value (in this case "sales")

df[df["Dept"]=="sales"]["Salaries"].mean()

25.0

Upvotes: 0

Views: 57

Answers (1)

famargar
famargar

Reputation: 3448

df.groupby("Dept")["Salaries"].mean()['sales']

but why would you want that? It is probably more CPU-intensive

Upvotes: 1

Related Questions