Naga Nir
Naga Nir

Reputation: 61

Mean of certain column data in python

enter image description here

Hi guys, just wondering if I could get the mean of the "Tax" column for a certain "Year". So for example: I would want to get the mean of the "Tax" for the "Year" 2020 and 1999.

thanks in advance

Upvotes: 1

Views: 42

Answers (1)

ansev
ansev

Reputation: 30940

You can filter rows and then calculate the mean, In this case we use Series.isin in order to filter:

df.loc[df['year'].isin([1999, 2000]), "Tax"].mean()

If you want calculate mean for each year:

df.groupby("year")["Tax"].mean().loc[[1999, 2000]]

Upvotes: 1

Related Questions