我有一个包含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更多推荐
通过乘法增加特定的行,直到列的总和满足条件
发布评论