Excel to Python: IFERROR Function - A Complete Guide | Mito
Home Icon
divider

Functions

divider

Conditional

divider

IFERROR

How to Use Excel's IFERROR Function in Pandas

In Excel, the IFERROR function is used to catch and handle errors produced by other formulas. If the formula does not produce an error, IFERROR will simply return the original result. If there is an error, you can specify an alternate value or action.

In Python's pandas library, there isn't a direct IFERROR function. Instead, various techniques and methods are employed to achieve similar error handling. Below are some equivalent ways to perform IFERROR-like operations in pandas.

Mito is an open source library that lets you write Excel formulas in Python. Either write the formula directly in Python or use the IFERROR formula in the Mito Spreadsheet and generate the equivalent Python code automatically.

Mito's IFERROR function works exactly like it does in Excel. That means you don't need worry about managing data types, handling errors, or the edge case differences between Excel and Python formulas.

Install Mito to start using Excel formulas in Python.

# Import the mitosheet Excel functions
from mitosheet.public.v3 import *;

# Use Mito's IFERROR function
df['A'] = FILLNAN(df['A'], 0)
Copy!
Clipboard

In pandas, there isn't a direct IFERROR function. Below are some equivalent ways to perform IFERROR-like operations in pandas.

Often when formula 'error' in pandas they return NaN values. NaN stands for 'Not a Number' and are used to represent missing or undefined values.

In pandas, you can use the fillna() function to replace NaN values with a specified value, similar to how you use the IFERROR formula in Excel.

Let's say that you have a DataFrame with a column that contains NaN values. You can use the fillna() function to replace the NaN values with 0.

# Fill NaN values with 0
df['column_name'].fillna(0)
Copy!
Clipboard

In pandas, you can use the isna() function to check if a value is NaN. It returns true if the value is NaN, and false otherwise. You can then use the result of the isna() function in a conditional to perform an action based on whether the value is NaN or not.

import numpy as np

# Create a custom function that labels values
def label_values(value):
    if np.isnan(value):
        return "Value is None"
    else:
        return "Value is not None"

# Apply the function to the DataFrame
df['Category'] = df['A'].apply(label_values)
Copy!
Clipboard

In pandas, you can use try catch statements to gracefully handle errors. You can use the try statement to run a block of code that might produce an error. If an error is produced, the except statement will run. You can then use the except statement to handle the error in a specified way.

# Using Try Catch Statements to Gracefully Handle Errors
try:
    result = 100 / denominator
except:
    result = 0
Copy!
Clipboard

Below are some common mistakes to avoid when using IFERROR in Excel.

Both None and NaN are special values in Python that represent missing or null data. However, they come from different parts of the Python ecosystem and have different behaviors and use cases.

None is a built-in constant in Python that indicates the absence of a value or a null value. It is of type NoneType.

NaN stands for Not a Number and is a special floating-point value used primarily within the numpy and pandas libraries.

To check if a value is None, you can use the `is None` syntax. To check if a value is NaN, you can use the isna() function.

import numpy as np

# Check if a value is None
if value is None:
    print('Value is None')

# Check if a value is NaN
if np.isnan(value):
    print('Value is NaN')
Copy!
Clipboard

The IFERROR function checks the first value (formula or expression) and returns that value if no error is found. If an error is found, it returns the second value.

=IFERROR(value, value_if_error)

IFERROR Excel Syntax

ParameterDescriptionData Type
valueThe value to check for an error.Any
value_if_errorThe value to return if an error is found.Any

Examples

FormulaDescriptionResult
=IFERROR(1/0, 0)Returns 0 because the formula 1/0 produces an error.0
=IFERROR(1/1, 0)Returns 1 because the formula 1/1 does not produce an error.1
=IFERROR(#N/A, 0)Returns 0 because #N/A is an error.0

Don't re-invent the wheel. Use Excel formulas in Python.

Install Mito

Don't want to re-implement Excel's functionality in Python?

Automate analysis with Mito