Frank Jimenez
Frank Jimenez

Reputation: 369

Using np.nan() to find average of each row and column

I have a df with 10 columns with GDP between 2006 and 2015 of 15 different countries. I am trying to get the mean GDP from the last ten years. For example, Germany's average GDP from 2006 to 2015. This is the code I tried to use:

avgGDP = pd.DataFrame(Top15,columns=['2006','2007','2008','2009', '2010', '2011', '2012', '2013', '2014', '2015'])
avgGDP['avg10'] = np.nanmean(avgGDP)

However, I get the same mean value for each country i.e. Germany 1,000,000; Spain 1,000,000; UK 1,000,000 etc.

Any thoughts on how to get individual mean for each country?

EDITED:

Here's a screenshot of Top15:

enter image description here

Upvotes: 0

Views: 190

Answers (1)

Colton Neary
Colton Neary

Reputation: 90

Can you just filter the null values and then run a mean method?

df = (avgGDP.dropna( inplace = True )).mean()

It seems to me you are running a mean on the entire dataframe to get an array and then trying to make that array into a column. The better approach could also be to transpose the data frame and insert as a column:

avgGDP = avgGDP.T
avgGDP['newRow'] = np.nanmean(avgGDP)
avgGDP.T

Upvotes: 1

Related Questions