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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With