Home > Back-end >  Get mean based on the other columns
Get mean based on the other columns

Time:11-06

Suppose I've a dataset which has 2 columns named : 'rate' 'rest_type' and they have values like :

rate   rest_type
3.4    dining
4      casual
3      cafe   
3.4    delivery
4      mexian
3      italian
4.4    indian
4      south_indian
3      cafe

Now I want to calculate the mean of only the rate which has corresponding columns as 'dining' & 'cafe' only

How can i do that ? Thanks in advance

Please check the above

CodePudding user response:

Use boolean indexing:

out = df.loc[df['rest_type'].isin(['cafe', 'dining']), 'rate'].mean()

output: 3.1333333333333333

Intermediate:

df.loc[df['rest_type'].isin(['cafe', 'dining']), 'rate']

0    3.4
2    3.0
8    3.0
Name: rate, dtype: float64
  • Related