Pandas DataFrame Comparison Operators and Combine – Part 3

The Pandas DataFrame has several binary operator methods. When applied to a DataFrame, these methods combine two DataFrames and return a new DataFrame with the appropriate result. This is Part 3 of the following series on Pandas DataFrame operators: Part 1: Pandas DataFrame Arithmetic Operators Part 2: Pandas DataFrame Reverse Methods Part 3: Pandas DataFrame … Read more

How to Override the “not” Operator in a Python Magic Method?

Short Answer: To override the logical not operator for a custom Python class My_Class, redefine the dunder method My_Class.__bool__() to return your custom Boolean value. This ensures that bool(x) on a My_Class object x returns either True or False. The operation not x will then return the inverse Boolean value, i.e, not x evaluates to … Read more

Python __le__() Magic Method

Short summary: To customize the behavior of the less than or equal to operator x <= y, override the __le__() dunder method in your class definition. Python internally calls x.__le__(y) to obtain a return value when comparing two objects using x <= y. The return value can be any data type because any value can … Read more

How to Make Division by Zero to Zero in Python?

In Python, division by zero generates the exception ZeroDivisionError:  division by zero.  This is because in mathematics, division by zero is undefined. Today we’ll go over some ways to avoid the zero division error in Python and force it to return a zero. First, we’ll look into exception handling in Python. Second, we’ll examine different … Read more

Python __lt__() Magic Method

Short summary: To customize the behavior of the less than operator x < y, override the __lt__() dunder method in your class definition. Python internally calls x.__lt__(y) to obtain a return value when comparing two objects using x < y. The return value can be any data type because any value can automatically converted to … Read more

Python __gt__() Magic Method

Short summary: To customize the behavior of the greather than operator x > y, override the __gt__() dunder method in your class definition. Python internally calls x.__gt__(y) to obtain a return value when comparing two objects using x > y. The return value can be any data type because any value can automatically converted to … Read more

Python __eq__ Magic Method

To customize the behavior of the equality operator x == y, override the __eq__() dunder method in your class definition. Python internally calls x.__eq__(y) to compare two objects using x == y. If the __eq__() method is not defined, Python will use the is operator per default that checks for two arbitrary objects whether they … Read more