0

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.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Richard
  • 62,943
  • 126
  • 334
  • 542
  • [Duplicate Question](https://stackoverflow.com/questions/24101524/finding-median-of-list-in-python) – mccurcio Nov 19 '22 at 02:29

1 Answers1

2

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mozway
  • 194,879
  • 13
  • 39
  • 75