通过乘法增加特定的行,直到列的总和满足条件

编程入门 行业动态 更新时间:2024-10-24 10:17:28
本文介绍了通过乘法增加特定的行,直到列的总和满足条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个包含4列的数据框,我想执行以下步骤(最好在一个代码中): -过滤4列总和低于0.9的行 -将每行中的每个单元格相乘,以使该行的总和为0.9 -如果任何单元格中都有0,则此单元格保持不变(因为0与任何值的乘积保持为0) -最后显示所有行,以及未更改的行

I have a dataframe with 4 columns and I want to do the following steps (ideally in one code): - Filter rows where the sum of the 4 columns is lower than 0.9 - Multiply each cell in each row so that the sum of the row is 0.9 - In case there is a 0 in any cell, this cell stays unchanged (as multiplying 0 with anything remains 0) - At the end display all rows, also the ones that were not changed

这是一个示例数据框:

df = pd.DataFrame({'A':[0.03, 0.0, 0.7], 'B': [0.1234, 0.4, 0.333], 'C': [0.5, 0.4, 0.0333]}) print (df) Name A B C 0 Bread 0.03 0.1234 0.5000 1 Butter 0.00 0.4000 0.4000 2 Cheese 0.70 0.3330 0.0333 Sum = df["A"]+df["B"]+df["C"] print (Sum) 0 0.6534 1 0.8000 2 1.0663

现在该算法仅应影响第0行和第1行

Now only rows 0 and 1 should be affected by the algorithm

我使用了在这里部分起作用的那个:

I had used this one which worked partly here:

df = df4.mul(0.9/df4.sum(axis=1),axis=0)

但是我现在知道如何仅使用A到C列,以及如何首先按总和低于0.9的行过滤,然后如何再次显示所有行.

But I do now know how to work only with the columns A to C and how I can first filter by the rows where the sum is below 0.9 and then how to show all rows again.

所以我想要的结果是这样的:

So my desired outcome is something like this:

print (df) Name A B C 0 Bread 0.0414 0.170292 0.690000 1 Butter 0.0000 0.452000 0.452000 2 Cheese 0.70 0.3330 0.0333

重要的是,所有列(包括产品列)和行都应仍然存在,并且格式应为包含所有行的数据框.我仅在下面添加了sum函数,以了解它们的总和为0.9或更大.

Important, all columns (including product column) and rows should still be there and the format be a dataframe with all of the rows. I only added the sum function below to see that they add up to 0.9 or more.

Sum = df["A"]+df["B"]+df["C"] print (Sum) 0 0.9 1 0.9 2 1.0663

推荐答案

要将中间值保存在新的数据帧df2中:

To save the intermediate values in a new dataframe df2:

df2 = df.apply(lambda x : x if x.sum() > 0.9 else x.mul(0.9/x.sum()), axis=1)

df2是:

df2 A B C 0 0.041322 0.169972 0.688705 1 0.000000 0.450000 0.450000 2 0.700000 0.333000 0.033300

如果您这样做:

df2.sum(axis=1)

您得到:

0 0.9000 1 0.9000 2 1.0663

更多推荐

通过乘法增加特定的行,直到列的总和满足条件

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

发布评论

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

>www.elefans.com

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