pandas 将计算的行添加到数据框的底部

编程入门 行业动态 更新时间:2024-10-26 01:25:20
本文介绍了 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 将计算的行添加到数据框的底部

本文发布于:2023-10-31 07:52:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545492.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!