Data Handling using Pandas - I
Maths on Series
Predict results of operations between Series, with and without fill_value.
When you do arithmetic on two Series, Pandas matches values by index label, not by position.
If a label is missing in either Series, the result at that label is NaN (Not a Number), and the result's data type becomes float64.
Example: s1 has labels a and b with values 1 and 2; s2 has labels a and c with values 10 and 30. s1 + s2 gives a: 11.0, b: NaN, c: NaN.
To avoid NaN, use the methods add(), sub(), mul() and div() with a fill_value. It replaces a missing value before the operation. s1.add(s2, fill_value=0) gives a: 11.0, b: 2.0, c: 30.0.
Be careful with division: dividing by a filled-in 0 gives inf (infinity). Labels are matched first, then the operation is done.
Practise this lesson
Questions that get harder as you improve, a Tutor that helps without giving the answer away, and progress you can see. Free for every learner.
No account, no email address. Opens the real product.