How to get the mean and median of derived value

Question:

I have a DataFrame of houses in different towns:

data = [
  ['Oxford', 2016, True],
  ['Oxford', 2016, True],
  ['Oxford', 2018, False],
  ['Cambridge', 2016, False],
  ['Cambridge', 2016, True],
  ['Brighton', 2019, True],
]
df = pd.DataFrame(data, columns=['town', 'year_built', 'is_detached'])

I want to get the mean and median number of houses per town.

How can I do this?

I know how to get the mean (hackily):

len(df) / len(df.town.value_counts())

But I don’t know how to get the median.

Asked By: Richard

||

Answers:

Use value_counts to get the number of houses per town, and then agg with 'mean' and 'median':

df['town'].value_counts().agg(['mean', 'median'])

Output:

mean      2.0
median    2.0
Name: town, dtype: float64
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.