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

Functions

divider

Text

divider

FIND

How to Use Excel's FIND Function in Pandas

Excel's FIND function returns the position of a substring within a string. It's used in various scenarios like string parsing, pattern detection, and string manipulation.

This page explains how to replicate Excel's FIND function in Python using 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 FIND formula in the Mito Spreadsheet and generate the equivalent Python code automatically.

Mito's FIND 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 FIND function
df['position'] = FIND(df['text_to_search'], 'text to search for')
Copy!
Clipboard

To replicate the FIND function in Excel using Python and pandas, we use the `.str.find()` method. Here are some common implementations:

Just like how you would use `=FIND("M", A1)` in Excel to find the position of 'M' in cell A1, in pandas you can find the position of a substring in a string column using the `.str.find()` method.

Keep in mind, however, that there are two important differences between Excel's FIND function and pandas' `.str.find()` method:

Firstly, the `.str.find()` method returns -1 if the substring is not found, whereas Excel's FIND function returns a #VALUE!. That means the way you handle strings that don't contain your substring will be different in pandas and Excel.

Secondly, the `.str.find()` method is 0 indexed, whereas Excel's FIND function is 1 indexed. In other words, if you are searching for 'M' in the string 'Mito', pandas' `.str.find()` method will return 0 wherease Excel's FIND method will return 1. That means you'll need to adjust the result of the `.str.find()` method by adding 1 to it to get the same result as Excel's FIND function.

# Find the position of 'M' in a string column
df['position'] = df['text'].str.find('M')
Copy!
Clipboard

Remember that there are two important differences between Excel's FIND function and pandas' `.str.find()` method: 1) the `.str.find()` method returns -1 if the substring is not found, whereas Excel's FIND function returns a #VALUE! 2) the `.str.find()` method is 0 indexed, whereas Excel's FIND function is 1 indexed.

To recreate Excel's FIND behavior in Python, you can use the following approach:

# Create a function that replicates Excel's FIND function
def excel_find(substring, series):
    # Use the .str.find() method to get the position
    positions = series.str.find(substring).replace(-1, None)

    # Adjust for 1-based indexing
    positions += 1

    return positions

# Use the function to find the position of 'M' in a string column
df['position'] = excel_find('M', df['text'])
Copy!
Clipboard

To start looking for the substring from the end of the string, you can use the `.str.rfind()` method instead of `.str.find()`.

If there is only one occurence of the substring in the string, `.str.rfind()` will return the same result as `.str.find()`. However, if there are multiple occurences of the substring in the string, `.str.rfind()` will return the position of the last occurence of the substring, whereas `.str.find()` will return the position of the first occurence of the substring.

# Find the first occurence of 'o' in each row of the 
# text column, starting from the end of the string
df['position'] = df['text'].str.rfind('o')
Copy!
Clipboard

By default, both Excel's FIND function and pandas' `.str.find()` method are case-sensitive. However, in pandas, if you want to perform a case-insensitive search, you can convert both the DataFrame column and the substring to lowercase (or uppercase) before searching.

Here's how you perform a case-insensitive search for 'mito' in a DataFrame column:

df[position'] = df['text'].str.lower().find('mito')
Copy!
Clipboard

Using the `.str.find()` method in pandas requires careful attention to some nuances. Here are common pitfalls and how to address them:

In Excel, the FIND function starts counting from 1. In Python and pandas, the count starts from 0.

If `.str.find()` doesn't find the substring, it returns -1. This can be surprising if you're expecting an error or a different value.

The `.str.find()` method is case-sensitive by default -- just like Excel. This is a common source of errors when not realizing that your data has both uppercase and lowercase versions of the text you're searching for.

The easiest way to perform a case-insensitive search is to convert both the DataFrame column and the substring to lowercase (or uppercase) before searching.

# Performing a case-insensitive search
df['position'] = df['ColumnName'].str.lower().find('mito')
Copy!
Clipboard

The FIND function in Excel takes a substring and a string as arguments and returns the starting position of the first occurrence of the substring in the string.

=FIND(find_text, within_text, [start_num])

FIND Excel Syntax

ParameterDescriptionData Type
find_textThe substring you want to find.text
within_textThe text in which you want to search.text
start_num(Optional) The position in the string to start the search.number

Examples

FormulaDescriptionResult
=FIND("M", "Mito")Find the position of 'M' in 'Mito'.1
=FIND("o", "Mito")Find the position of 'o' in 'Mito'.4

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