计算新列作为其他列 pandas 的平均值 [英] Calculate new column as the mean of other columns pandas

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

问题描述

我有一个此数据框:

,我想计算一个新列,作为salary_1salary_2salary_3的平均值.

and I would like to calculate a new columns as de the mean of salary_1, salary_2and salary_3.

df = pd.DataFrame({'salary_1':[230,345,222],'salary_2':[235,375,292],'salary_3':[210,385,260]})

      salary_1     salary_2    salary_3
0        230           235        210
1        345           375        385
2        222           292        260

如何在熊猫中以最有效的方式做到这一点?实际上,我还有更多的专栏文章,而且我不想一一撰写.

How can I do it in pandas in the most efficient way? Actually I have many more columns and I don't want to write this one by one.

类似这样的东西:

      salary_1     salary_2    salary_3     salary_mean
0        230           235        210     (230+235+210)/3
1        345           375        385       ...
2        222           292        260       ...

谢谢!

推荐答案

解决此问题的简单方法如下所示:

an easy way to solve this problem is shown below :

col = df.loc[: , "salary_1":"salary_3"]

其中"salary_1"是开始列的名称,而"salary_3"是结束列的名称

where "salary_1" is the start column name and "salary_3" is the end column name

df['salary_mean'] = col.mean(axis=1)
df

这将为您提供一个带有新列的新数据框,该列将显示所有其他列的均值 当您拥有大量的列时,这种方法非常有用;当您只需要对某些选定的列而不是全部执行时,这种方法也很有用.

This will give you a new dataframe with a new column that shows the mean of all the other columns This approach is really helpful when you are having a large set of columns or also helpful when you need to perform on only some selected columns not on all.

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

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