feedthemachine
feedthemachine

Reputation: 620

find a mean value for each row in dataframe and return mean result

I got the following DF: http://prntscr.com/f72cbm

def answer():
    Top15 = one()
    timefr = ['2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015']
    rng = range(0, 9)
       for i in rng:
           for k,v in Top15[timefr].iloc[[i]].iteritems():
                print(k, v)

function returns many Series of the following kind:

2006 Country
China    3.992331e+12
Name: 2006, dtype: float64
2007 Country
China    4.559041e+12
Name: 2007, dtype: float64

Year, Countryname, value.
I possibly could iterate through each Series and sum all values up and then have them divided and so on, but is there a more "pandorable" way of doing so?
I would also like to skip NaN value

Upvotes: 1

Views: 83

Answers (1)

fuglede
fuglede

Reputation: 18201

If I'm reading the question correctly, all you want to do is df.mean(axis=1); for example,

In [4]: df
Out[4]: 
   1980  1981
a     1     4
b     2     5

In [5]: df.mean(axis=1)
Out[5]: 
a    2.5
b    3.5
dtype: float64

Upvotes: 1

Related Questions