Vikas Sharma
Vikas Sharma

Reputation: 35

Get mean based on the other columns

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

Upvotes: 1

Views: 26

Answers (1)

mozway
mozway

Reputation: 262634

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

Upvotes: 2

Related Questions