计算平均值列和标准偏差列并将其添加到数据框 [英] Calculating and adding average and standard deviation columns to a data frame
本文介绍了计算平均值列和标准偏差列并将其添加到数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有:
df = pd.DataFrame({'A1': [0.1,0.5,3.0, 9.0], 'A2':[2.0,4.5,1.2,9.0]})
我想在数据框中添加2列,以计算平均值和标准偏差,例如:
I would like to add 2 columns to the data frame that calculate the average and standard deviation like:
A1 A2 Mean Stddev
0 0.1 2.0 0 0
1 0.5 4.5 0 0
2 3.0 1.2 0 0
3 9.0 9.0 0 0
推荐答案
让我们尝试使用assign
并使用mean
和std
并使用参数axis=1
:
Let's try with assign
using mean
and std
with parameter axis=1
:
df.assign(Mean=df.mean(1), Stddev=df.std(1))
输出:
A1 A2 Mean Stddev
0 0.1 2.0 1.05 1.343503
1 0.5 4.5 2.50 2.828427
2 3.0 1.2 2.10 1.272792
3 9.0 9.0 9.00 0.000000
编辑评论/添加CpK:
df.assign(mean=df.mean(1),stddev=df.std(1)).eval('Cpk = (mean + stddev) / A2')
输出:
A1 A2 mean stddev Cpk
0 0.1 2.0 1.05 1.343503 1.196751
1 0.5 4.5 2.50 2.828427 1.184095
2 3.0 1.2 2.10 1.272792 2.810660
3 9.0 9.0 9.00 0.000000 1.000000
这篇关于计算平均值列和标准偏差列并将其添加到数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文