Python 计算熊猫的行平均值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33750326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:54:56  来源:igfitidea点击:

Compute row average in pandas

pythonpandas

提问by user308827

       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)

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

但是,这也取消了 Region 列。我如何计算均值并保留 Region 列

采纳答案by Alexander

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

您可以指定一个新列。您还需要计算沿行的平均值,因此请使用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

回答by Rahul

If you are looking to average column wise. Try this,

如果您希望对列进行平均。尝试这个,

df.drop('Region', axis=1).apply(lambda x: x.mean())

# it drops the Region column
df.drop('Region', axis=1,inplace=True)

回答by pabloverd

I think this is what you are looking for:

我认为这就是你要找的:

df.drop('Region', axis=1).apply(lambda x: x.mean(), axis=1)