I have a CSV file with a huge list of house pricing information. I need to calculate the mean price overall and the most common zip codes. My code does these things fine so far but i need to split the housing now into its various types (of which there are about 5) and i need to calculate the mean price for these individual types.
Asked
Active
Viewed 244 times
0
-
groupby housetype and calculate the mean of the groups? – Patrick Artner Apr 30 '20 at 12:29
-
Probably a dupe of [pandas-group-by-in-group-by-and-average](https://stackoverflow.com/questions/30328646/python-pandas-group-by-in-group-by-and-average) – Patrick Artner Apr 30 '20 at 12:30
-
I've been trying to implement some of the code suggested in that post but I am getting the wrong output. The groupby function doesn't seem to be grouping the correct column as the output seems to still contain info from other columns. – Sly_Lamp Apr 30 '20 at 12:48
2 Answers
1
If you have a column containing the types of house, you can just group for that column ad then compute the mean:
avgs = df.groupby('Housetype').mean()

Mazziotti Raffaele
- 411
- 4
- 13
-1
Separate them to different dataframes, e.g.
df1 = df[df['HouseType'] == 1]
will create a new dataframe with only type1 houses

gtomer
- 5,643
- 1
- 10
- 21