简体   繁体   English

我如何将每个数字除以python中每行的总和

[英]how do I divide each number by sum of each row in python

在此处输入图片说明

how do I divide each number by the sum?我如何将每个数字除以总和? (skipping zeros) I'd like to divide each row by it's sum (跳过零)我想将每一行除以它的总和

ex.前任。 0(number on 0 column)/2(sum column) 0(第 0 列上的数字)/2(总和列)

0 1 2 3 4 5 6 7 ... sum
0 0 0 0 1 0 0 0 ...  2

result结果

0 1 2 3 4   5 6 7 ... sum
0 0 0 0 0.5 0 0 0      2

You can try something like this你可以试试这样的

#### this will contain everyother column except sum
required_columns = df.columns[~df.contains.str.contains('sum')]
### regex can also be used with contains , I m here assuming you all other column will not be named as sum , for which the division is to be performed


for col in required_colums:
    print (f'---------- {col} --------')
    df.loc[:,col] = df.loc[:,col]/df.loc[:,'sum']

You can also give this to get the same answer.您也可以给出这个以获得相同的答案。

df.iloc[:,:-1] = df.apply(lambda r: r/r['sum'] if r['sum'] != 0 else r['sum'],axis=1).round(2)

The output of this will be:输出将是:

Source df:来源 df:

   0  1  2  3  4  5  6  7  sum
0  0  0  0  0  1  0  0  0    2
1  0  0  0  6  0  0  0  0   18
2  0  0  0  0  1  0  0  0    0
3  0  0  3  0  0  0  4  0    1

This will result in:这将导致:

     0    1    2     3    4    5    6    7  sum
0  0.0  0.0  0.0  0.00  0.5  0.0  0.0  0.0    2
1  0.0  0.0  0.0  0.33  0.0  0.0  0.0  0.0   18
2  0.0  0.0  0.0  0.00  0.0  0.0  0.0  0.0    0
3  0.0  0.0  3.0  0.00  0.0  0.0  4.0  0.0    1

Here is the explanation for the above code:下面是对上述代码的解释:

On the left hand side of the equation, I have iloc.在等式的左侧,我有 iloc。 You can get more documentation of iloc here.您可以在此处获取有关iloc 的更多文档。

df.iloc[:,:-1]

Here I am picking all the rows (first set of :,).在这里,我选择了所有行(第一组:,)。 The second set is the columns.第二组是列。 I am having the right hand side computational value assigned to all but the last column which is the sum column.我将右手边的计算值分配给除最后一列(即sum列)之外的所有列。 I dont want to replace that value.我不想替换那个值。

df.apply will process the dataframe one row at a time. df.apply 将一次处理一行数据帧。 see examples of df.apply here在此处查看df.apply 的示例

Here I am picking the first row (r) and processing it.在这里,我选择第一行 (r) 并对其进行处理。 You wanted to compute column (x) / column('sum').您想计算列 (x) / 列 ('sum')。 Thats what i am doing.这就是我正在做的。 It does this for each column in the row.它对行中的每一列执行此操作。

I am also checking if r['sum'] is not equal to zero to avoid division by zero error.我也在检查r['sum']是否不等于零以避免除以零错误。 If the value of r['sum'] is zero, then i am sending r['sum'] (or zero).如果r['sum']值为零,那么我将发送r['sum'] (或零)。

A DataFrame object has two axes: “axis 0” and “axis 1”. DataFrame 对象有两个轴:“axis 0”和“axis 1”。 “axis 0” represents rows and “axis 1” represents columns. “axis 0”代表行,“axis 1”代表列。 I am using axis = 1 to traverse through the row instead of values in each column.我使用axis = 1遍历行而不是每列中的值。

Hope this explanation helps.希望这个解释有帮助。

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

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