pandas 将计算的行添加到数据框的底部 [英] Pandas add calculated row to bottom of dataframe

查看:62
本文介绍了 pandas 将计算的行添加到数据框的底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我拥有的一个数据帧的一小部分示例,我想在其底部添加一个计算所得的行:

Below is a small sample of a dataframe I have, and I want to add a calculated row to the bottom of it:

sch     q1     q2     q3
acc     Yes    Yes    No
acc     Yes    No     No
acc     Yes    No     No
acc     Yes    Yes    Yes

我想在底部添加一行,这将为我提供每一列为是"的值的百分比,因此如下所示.

I want to add a row at the bottom that will give me the percentage of values that are 'Yes' for each column, so that it would look like below.

sch     q1     q2     q3
acc     Yes    Yes    No
acc     Yes    No     No
acc     Yes    No     No
acc     Yes    Yes    Yes
acc     1.00   0.5    0.25

任何帮助将不胜感激.

推荐答案

我看到了您的lambda并提出了一个纯粹的熊猫解决方案:

I see your lambda and raise a pure pandas solution:

df.append(df.eq('Yes').mean(), ignore_index=True)

您没有指定sch列应如何处理,因此我将其忽略.在我当前的解决方案中,此列将获得值0.

You don't specify what should happen to the sch column, so I ignored it. In my current solution this column will get the value 0.

这篇关于 pandas 将计算的行添加到数据框的底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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