1

I want to use .groupby().mean() on this DataFrame, however, it doesn't omit the NaN values.

Here is the picture of my DataFrame

City_Zhvi_AllHomes

'Quarters' is my last row so I do transpose to make it the last column, and groupby 'Quarters', then do .mean() for all the counties. I expect to store the new DataFrame in a new variable, with column indexes as county names, and row indexes as 'Quarters', each cell is the mean of 3 cells that share the same County and same 'Quarters' in the previous DataFrame.

So I do:

ANSWER = City_Zhvi_AllHomes.T.groupby('Quarters').mean().T

But I got:

DataError: No numeric types to aggregate

It will work if I do .fillna(0) before .groupby(), here is the picture for that (except I transpose the row and column again)

answer

But I can't do that since it will affect the result for the mean. So what should I do to tell .mean() to ignore NaN?

Fredrik Widerberg
  • 3,068
  • 10
  • 30
  • 42
Omegalpha
  • 51
  • 3
  • Welcome to SO. Please don't includes images. Provide a [mcve]. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – jpp Jul 10 '18 at 10:41
  • I think need `ANSWER = groupby('Quarters').mean()` – jezrael Jul 10 '18 at 12:14
  • 1
    I finally got it. Just in case someone might need it. I use **df.astype('float64')** to change the type from 'object' to 'float64', then the .mean() works, without saying "No numeric types to aggregate". – Omegalpha Jul 11 '18 at 05:23

0 Answers0