Get startedGet started for free

Calculating means by category

A good way to explore categorical variables further is to calculate summary statistics for each category. For example, you can calculate the mean and median of your response variable, grouped by a categorical variable. As such, you can compare each category in more detail.

Here, you'll look at grouped means for the house prices in the Taiwan real estate dataset. This will help you understand the output of a linear regression with a categorical variable.

taiwan_real_estate is available as a pandas DataFrame.

This exercise is part of the course

Introduction to Regression with statsmodels in Python

View Course

Exercise instructions

  • Group taiwan_real_estate by house_age_years and calculate the mean price (price_twd_msq) for each age group. Assign the result to mean_price_by_age.
  • Print the result and inspect the output.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Calculate the mean of price_twd_msq, grouped by house age
mean_price_by_age = ____.____(____)[____].____

# Print the result
print(____)
Edit and Run Code