python3 -c 'import pandas as pd; df=pd.read_csv("data.csv"); print(df.groupby("dept")["salary"].mean())'

Category: Data Science & Analysis

python3 -c 'import pandas as pd; df=pd.read_csv("data.csv"); print(df.groupby("dept")["salary"].mean())'

Average salary per dept with pandas groupby

The groupby method splits the DataFrame by the dept column's values, the square brackets select the salary column, and mean() aggregates each group. The result is a per-department average, the pandas equivalent of a SQL GROUP BY. Chain sort_values() to order the groups by their means. This is the standard pandas idiom for grouped summaries.
Looking for more? Search all 7,657 commands — works offline, in English or Spanish, and fixes typos.