熊猫数据框中的行平均

发布于 2025-02-10 18:08:38 字数 859 浏览 1 评论 0原文

对于以下数据框架,

              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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甩你一脸翔 2025-02-17 18:08:38

python slice end值是独有的,而不是包容性,因此您有效地采用aacc的平均值。您需要将end值增加1,以包括bbdd在每个平均值:

df['mean1'] = df.iloc[:, 0:2].mean(axis=1)
df['mean2'] = df.iloc[:, 2:4].mean(axis=1)

输出:输出:

           AA         BB        CC        DD       mean1     mean2
0  445.664800  36.120182  1.707122  0.332993  240.892491  1.020058
1  397.461500   8.733798  0.346957  0.332993  203.097649  0.339975
2    4.750258   5.197949  0.365944  0.332993    4.974104  0.349468

Python slice end values are exclusive, not inclusive, so you are effectively taking the mean of AA and CC. You need to increase your end values by 1 to include BB and DD in each mean:

df['mean1'] = df.iloc[:, 0:2].mean(axis=1)
df['mean2'] = df.iloc[:, 2:4].mean(axis=1)

Output:

           AA         BB        CC        DD       mean1     mean2
0  445.664800  36.120182  1.707122  0.332993  240.892491  1.020058
1  397.461500   8.733798  0.346957  0.332993  203.097649  0.339975
2    4.750258   5.197949  0.365944  0.332993    4.974104  0.349468
枕梦 2025-02-17 18:08:38

在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文