按列总和划分 Pandas 数据框中的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40940423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:33:57  来源:igfitidea点击:

Divide Column in Pandas Dataframe by Sum of Column

pythonpandasdataframesum

提问by spacedinosaur10

I have a dataframe where I would like to divide each row within column A by the sum of column A and make that a new column within the dataframe.

我有一个数据框,我想将 A 列中的每一行除以 A 列的总和,并将其作为数据框中的一个新列。

Example:

        Col A   New Col
        2       .22
        3       .33
        4       .44
Total = 9       1.00

I tried to sum Col A and then tried to divide by 'Total' but because Total is not a column but a row, it did not work. I just get NaN for each row within the new column.

我尝试对 Col A 求和,然后尝试除以“Total”,但因为 Total 不是一列而是一行,所以它不起作用。我只是为新列中的每一行获取 NaN。

df['New Col']= (df['ColA']/df.loc['Total']) 

I know you can also probably integrate a sum calculation within the one line of code instead of creating a totals row as well but not sure how to do that and could not find anything online.

我知道你也可以在一行代码中集成一个总和计算,而不是创建一个总计行,但不知道如何做到这一点并且在网上找不到任何东西。

df['New Col']= (df['ColA']/df.sum()) 

Ideas?

想法?

回答by Steven G

df['new'] = df['ColA'] /  df['ColA'].sum()

should work

应该管用

回答by Clade

Another approach is to use transform:

另一种方法是使用transform

df['New Col'] = df['ColA'].transform(lambda x: x / x.sum())

回答by Andy

You are very close. You want to perform the sum()on the Col Aseries

你很亲近。你想sum()Col A系列上表演

df['New Col'] = df['Col A']/df['Col A'].sum()

Results in a dataframe that looks like this:

结果是一个如下所示的数据框:

>>> df
   Col A   New Col
0      2  0.222222
1      3  0.333333
2      4  0.444444

Now if you do df.sum()you get a Series with the totals per column:

现在,如果你这样做,df.sum()你会得到一个每列总数的系列:

>>> df.sum()
Col A      9.0
New Col    1.0
dtype: float64