计算 pandas 的平均行数 [英] Compute row average in pandas

查看:107
本文介绍了计算 pandas 的平均行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

       Y1961      Y1962      Y1963      Y1964      Y1965  Region
0  82.567307  83.104757  83.183700  83.030338  82.831958  US
1   2.699372   2.610110   2.587919   2.696451   2.846247  US
2  14.131355  13.690028  13.599516  13.649176  13.649046  US
3   0.048589   0.046982   0.046583   0.046225   0.051750  US
4   0.553377   0.548123   0.582282   0.577811   0.620999  US

在上面的数据框中,我想获取每一行的平均值.目前,我正在这样做:

In the above dataframe, I would like to get average of each row. currently, I am doing this:

df.mean(axis=0)

但是,这也消除了地区"列.如何计算均值并保留Region列

However, this does away with the Region column as well. how can I compute mean and also retain Region column

推荐答案

您可以指定一个新列.您还需要计算各行的平均值,因此请使用axis=1.

You can specify a new column. You also need to compute the mean along the rows, so use axis=1.

df['mean'] = df.mean(axis=1)
>>> df
       Y1961      Y1962      Y1963      Y1964      Y1965 Region       mean
0  82.567307  83.104757  83.183700  83.030338  82.831958     US  82.943612
1   2.699372   2.610110   2.587919   2.696451   2.846247     US   2.688020
2  14.131355  13.690028  13.599516  13.649176  13.649046     US  13.743824
3   0.048589   0.046982   0.046583   0.046225   0.051750     US   0.048026
4   0.553377   0.548123   0.582282   0.577811   0.620999     US   0.576518

这篇关于计算 pandas 的平均行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆