熊猫数据框中的行平均
对于以下数据框架,
AA BB CC DD
0 4.456648e+02 36.120182 1.707122 0.332993
1 3.974615e+02 8.733798 0.346957 0.332993
2 4.750258e+00 5.197949 0.365944 0.332993
我想按照所述计算范围的平均值,如所述在这里。例如,我写道:
df['mean1'] = df.iloc[:, 0:1].mean(axis=1)
df['mean2'] = df.iloc[:, 2:3].mean(axis=1)
so,平均1
是AA和BB和均值2
的平均值是CC和DD的平均值。但这并不如下所示:
AA BB CC DD mean1 mean2
0 4.456648e+02 36.120182 1.707122 0.332993 4.456648e+02 1.707122
1 3.974615e+02 8.733798 0.346957 0.332993 3.974615e+02 0.346957
2 4.750258e+00 5.197949 0.365944 0.332993 4.750258e+00 0.365944
如何解决?
For the following dataframe
AA BB CC DD
0 4.456648e+02 36.120182 1.707122 0.332993
1 3.974615e+02 8.733798 0.346957 0.332993
2 4.750258e+00 5.197949 0.365944 0.332993
I want to compute the average of rows with ranges as described here. For example, I wrote:
df['mean1'] = df.iloc[:, 0:1].mean(axis=1)
df['mean2'] = df.iloc[:, 2:3].mean(axis=1)
So, mean1
is averages for AA and BB and mean2
is the average of CC and DD. But it isn't as you see below:
AA BB CC DD mean1 mean2
0 4.456648e+02 36.120182 1.707122 0.332993 4.456648e+02 1.707122
1 3.974615e+02 8.733798 0.346957 0.332993 3.974615e+02 0.346957
2 4.750258e+00 5.197949 0.365944 0.332993 4.750258e+00 0.365944
How to fix that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
python slice
end
值是独有的,而不是包容性,因此您有效地采用aa
和cc
的平均值。您需要将end
值增加1,以包括bb
和dd
在每个平均值:输出:输出:
Python slice
end
values are exclusive, not inclusive, so you are effectively taking the mean ofAA
andCC
. You need to increase yourend
values by 1 to includeBB
andDD
in each mean:Output:
在Python中,切片具有语法
开始:stop:step
,结果从start(包含)到(但不包括)停止,步骤的增量。第三个参数默认为1。在您的情况下,行切片
0:1
仅包括第0行。同样,2:3
仅包括第2行。In python, slices have the syntax
start:stop:step
, where the result goes from start (inclusive) up to (but not including) stop, increments of step. The third argument is 1 by default.In your case, the row slice
0:1
includes only row 0. Similarly,2:3
includes only row 2.