Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does .div do in Pandas (Python)

Tags:

python

pandas

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

"Floating division of dataframe and other, element-wise (binary operator truediv)"

I'm not exactly sure what this means. Any help would be appreciated!

enter image description here

like image 849
bugsyb Avatar asked Jul 05 '17 23:07

bugsyb


2 Answers

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

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.

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.

like image 136
piRSquared Avatar answered Oct 21 '22 15:10

piRSquared


In the above example, what it is essentially doing is dividing 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 dividing the entire dataframe along 0 axis with the sum generated i.e. a row is divided by the sum of that row.

like image 26
Geetika Singla Avatar answered Oct 21 '22 16:10

Geetika Singla