Functions

Conditional

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.

### Use Mito's IFERROR function

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)
```

## Implementing the IFERROR function in Pandas#

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.

### Filling Missing Values with 0#

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)
```

### Casing on Missing Values in Conditionals#

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)
```

### Using Try Catch Statements to Gracefully Handle Errors#

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
```

## Common mistakes when using IFERROR in Python#

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

### Confusing NaN and None values#

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')
```

## Understanding the IFERROR Formula in Excel#

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

Parameter | Description | Data Type |
---|---|---|

value | The value to check for an error. | Any |

value_if_error | The value to return if an error is found. | Any |

### Examples

Formula | Description | Result |
---|---|---|

=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.**

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

## Edit a spreadsheet.

Generate Python.

Mito is the easiest way to write Excel formulas in Python. Every edit you make in the Mito spreadsheet is automatically converted to Python code.

View all 100+ transformations →