.div 在 Pandas (Python) 中有什么作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44937542/
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
What does .div do in Pandas (Python)
提问by bugsyb
I'm confused as to the highlighted line. What exactly is this line doing. What does .div do? I tried to look through the documentation which said
我对突出显示的行感到困惑。这条线到底在做什么。.div 有什么作用?我试图查看文件中说
"Floating division of dataframe and other, element-wise (binary operator truediv)"
“数据帧和其他元素的浮动划分(二元运算符 truediv)”
I'm not exactly sure what this means. Any help would be appreciated!
我不确定这意味着什么。任何帮助,将不胜感激!
回答by piRSquared
You can divide one dataframe by another and pandas will automagically aligned the index and columns and subsequently divide the appropriate values. EG df1 / df2
您可以将一个数据帧除以另一个数据帧,pandas 将自动对齐索引和列,然后划分适当的值。例如df1 / df2
If you divide a dataframe by series, pandas automatically aligns the series index with the columns of the dataframe. It maybe that you want to align the index of the series with the index of the dataframe instead. If this is the case, then you will have to use the div
method.
如果按系列划分数据帧,pandas 会自动将系列索引与数据帧的列对齐。也许您想将系列的索引与数据帧的索引对齐。如果是这种情况,那么您将不得不使用该div
方法。
So instead of:
所以而不是:
df / s
You use
你用
df.div(s, axis=0)
Which says to align the index of s
with the index of df
then perform the division while broadcasting over the other dimension, in this case columns.
这表示将 的索引s
与 的索引对齐,df
然后在其他维度上广播时执行除法,在本例中为列。
回答by Geetika Singla
In the above example, what it is essentially doing is diving pclass_xt on axis 0, by the array/series which (pclass_xt.sum(0)) has generated. In (pclass_xt.sum(0)), .sum is summing up values along the axis=1, which gives you the total of both survived and not survived along all the pclasses. Then, .div is simply diving the entire dataframe along 0 axis with the sum generated i.e. a row is divided by the sum of that row.
在上面的例子中,它本质上是通过 (pclass_xt.sum(0)) 生成的数组/系列在轴 0 上潜水 pclass_xt。在 (pclass_xt.sum(0)) 中,.sum 将沿轴 = 1 的值求和,这为您提供了沿所有 pclasses 幸存和未幸存的总和。然后,.div 只是沿着 0 轴将整个数据帧与生成的总和分开,即一行除以该行的总和。
Hope that helps!
希望有帮助!