Status
Done
Math Operations in Pandas Series (Titanic Dataset)
We'll use the age column from the Titanic dataset to demonstrate mathematical operations.
python
import pandas as pd
import seaborn as sns
# Load Titanic dataset
df = sns.load_dataset('titanic')‣
1. Basic Arithmetic Operations
‣
2. Reverse Arithmetic Operations (Reflected)
‣
3. Comparison Operations
Summary Table
Task | Method | Operator Equivalent |
Addition | add() | + |
Subtraction | sub() | - |
Multiplication | mul() | * |
True Division | truediv() | / |
Floor Division | floordiv() | // |
Modulo | mod() | % |
Exponentiation | pow() | ** |
Reverse Addition | radd() | other + series |
Reverse Subtraction | rsub() | other - series |
Reverse Multiplication | rmul() | other * series |
Reverse Division | rtruediv() | other / series |
Reverse Floor Division | rfloordiv() | other // series |
Reverse Modulo | rmod() | other % series |
Reverse Exponentiation | rpow() | other ** series |
Equal To | eq() | == |
Not Equal To | ne() | != |
Less Than | lt() | < |
Less Than or Equal To | le() | <= |
Greater Than | gt() | > |
Greater Than or Equal To | ge() | >= |
Between Two Values | between() | left <= series <= right |
Key Takeaways
- Use
add,sub,mul,truedivfor basic arithmetic. - Use
eq,lt,gtfor comparisons. - Reverse operations (
radd,rsub, etc.) are useful when the operation order matters. between()is a convenient way to check ranges.