Calculating maximum/minimum changes using pandas

Viewed 17

Suppose I had a data set containing job titles and salaries over the past three years and I want to go about calculating the difference in salary average from the first year to the last.

Using Pandas, how exactly would I go about doing that? I've managed to create a df with the average salaries for each year but I suppose what I'm trying to do is say "for data scientist, subtract the average salary from 2022 with the average salary from 2020" and iterate through all job_titles doing the same thing.


    work_year                   job_title  salary_in_usd
0        2020                AI Scientist   45896.000000
1        2020             BI Data Analyst   98000.000000
2        2020           Big Data Engineer   97690.333333
3        2020       Business Data Analyst  117500.000000
4        2020    Computer Vision Engineer   60000.000000
..        ...                         ...            ...
93       2022  Machine Learning Scientist  141766.666667
94       2022                NLP Engineer   37236.000000
95       2022      Principal Data Analyst   75000.000000
96       2022    Principal Data Scientist  162674.000000
97       2022          Research Scientist  105569.000000

1 Answers

Create a function which does the thing you want on each group:

def first_to_last_year_diff(df):
    diff = (
        df[df.work_year == df.work_year.max()].salary_in_usd
        - df[df.work_year == df.work_year.max()].salary_in_usd
    )

    return diff

Then group on job title and apply your function:

df.groupby("job_title").apply(first_to_last_year_diff)
Related