简体   繁体   English

Python Pandas 平均和总和冲突

[英]Python Pandas Average and Sum conflicts

I have a table that looks like this:我有一个看起来像这样的表:

桌子

The average and total rows are being calculated like so:平均行数和总行数计算如下:

df1.loc["Average"] = df1.mean()
df1.loc["Total"] = df1.sum()

Now, I realized that the problem here is that the Average is calculating properly, but the sum is including the Average row as well, which is not what I want.现在,我意识到这里的问题是平均值计算正确,但总和也包括平均值行,这不是我想要的。

Ideally, I'd like to see something more like a single.loc row that has sum() applied to理想情况下,我希望看到更类似于 single.loc 行的sum()应用于

`columns` ['Enageable R', 'R Responses', 'R Response Rate', 'Engageable Q',
                                'Q Responses', 'Q Response Rate']

and mean() applied to columns ['R Response Rate', 'Q Response Rate']mean()应用于columns ['R Response Rate', 'Q Response Rate']

So I would love to see something like this:所以我很想看到这样的东西:

Brand Engageable R可接合 R R Responses R 响应 Response Rate反应速度
Brand1品牌1 34 34 34 34 100.00% 100.00%
Brand2品牌2 34 34 34 34 100.00% 100.00%
Brand3品牌3 34 34 34 34 100.00% 100.00%
Total全部的 102 102 102 102 100.00% 100.00%

Use DataFrame.agg for processing both function separately and then add new rows by DataFrame.append :使用DataFrame.agg分别处理 function ,然后通过DataFrame.append添加新行

df = df.append(df.agg(['sum','mean']).rename({'sum':'Total','mean':'Average'}))

If need procesing only some columns:如果只需要处理一些列:

cols = ['Enageable R', 'R Responses', 'R Response Rate', 'Engageable Q',
                    'Q Responses', 'Q Response Rate']

df = df.append(df[cols].agg(['sum','mean']).rename({'sum':'Total','mean':'Average'}))

Use a copy:使用副本:

df2 = df1.copy(deep=True)

df2["Average"] = df1.mean()
df2["Total"] = df1.sum()

EDIT编辑

your post is a little confused.你的帖子有点混乱。

df1["Total"] = df1.sum()
df1["Average"] = df1[['R Response Rate', 'Q Response Rate']].mean(axis=1)

Here's what I found, with some credit due to @Wilian for the recommendation, although if there's a more efficient way to do this, let me know.这就是我发现的,感谢@Wilian 的推荐,尽管如果有更有效的方法可以做到这一点,请告诉我。

So I have a df1, and I created a blank df2 using:所以我有一个 df1,我使用以下方法创建了一个空白 df2:

df2 = pd.DataFrame(columns=['Enageable R', 'R Responses', 'R Response Rate', 'Engageable Q',
                        'Q Responses', 'Q Response Rate'])

Then I filled df2 with:然后我用 df2 填充:

df2.loc["Average"] = df1.mean()
df2.loc["Total"] = df1.sum()

Then I appended df2 to df1然后我将 df2 附加到 df1

df1 = df1.append(df2)

Which created the following:其中创建了以下内容:

表修订

So the sum and average columns are not including each other in the calculations of the other rows.因此,总和和平均列在其他行的计算中不包括彼此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM