Use the str accessor with square brackets:

df['col'] = df['col'].str[:9]

Or str.slice:

df['col'] = df['col'].str.slice(0, 9)
Answer from user2285236 on Stack Overflow
🌐
Statology
statology.org › home › pandas: how to get substring of entire column
Pandas: How to Get Substring of Entire Column
October 19, 2022 - #extract characters in positions 0 through 2 in points column df['points_substring'] = df['points'].astype(str).str[:2] #view updated DataFrame print(df) team points points_substring 0 Mavericks 120 12 1 Warriors 132 13 2 Rockets 108 10 3 Hornets 118 11 4 Lakers 106 10 · This time we’re able to successfully extract characters in positions 0 through 2 of the points column because we first converted it to a string. The following tutorials explain how to perform other common tasks in pandas:
🌐
Erikrood
erikrood.com › Python_References › substring_python.html
Right, left, mid equivalents (substrings) in Pandas
Looking to land a data science role? Practice interviewing with a few questions per week · Right, left, mid equivalents (substrings) in Pandas · import pandas as pd · Create some dummy data · string = '8754321' string · '8754321' · #right 2 characters string[-2:] · #left 2 characters ...
🌐
GeeksforGeeks
geeksforgeeks.org › get-the-substring-of-the-column-in-pandas-python
Get the substring of the column in Pandas-Python - GeeksforGeeks
July 10, 2020 - Example 1: We can loop through the range of the column and calculate the substring for each value in the column. Python3 1== # importing pandas as pd import pandas as pd # creating a dictionary dict = {'Name':["John Smith", "Mark Wellington", "Rosie Bates", "Emily Edward"]} # converting the dictionary to a # dataframe df = pd.DataFrame.from_dict(dict) # storing first 3 letters of name for i in range(0, len(df)): df.iloc[i].Name = df.iloc[i].Name[:3] df Output: Note: For more information, refer Python Extracting Rows Using Pandas Example 2: In this example we'll use str.slice().
🌐
Medium
medium.com › @amit25173 › extracting-substrings-from-pandas-columns-0a9cf37919ef
Extracting Substrings from Pandas Columns | by Amit Yadav | Medium
March 6, 2025 - Use negative indexing just like you would in Python. ... Alright, so slicing works, but what if you want something more precise? Maybe you need characters from position 2 to 5? That’s where .str.slice() comes in. Example: Extracting a Substring from a Specific Range
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Slice substrings from each element in columns | note.nkmk.me
April 23, 2022 - You can apply Python string (str) methods on the pandas.DataFrame column (= pandas.Series) with .str (str accessor). pandas: Handle strings (replace, strip, case conversion, etc.) This article describes how to slice substrings of any length from any position to generate a new column.
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas replace substring in dataframe
Pandas Replace Substring in DataFrame - Spark By {Examples}
June 6, 2025 - Replace a substring with another substring in Pandas. Replace a pattern of a substring with another substring using regular expression.
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › python pandas › get substring in pandas
How to Get Substring in Pandas | Delft Stack
February 2, 2024 - If we want to get the substring intel (first five characters), we will specify 0 and 5 as start and end indexes, respectively. We can also mention only the end index if we use the square bracket method because they have the same meaning.
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › Series › str › contains
Python Pandas Series str contains() - Check Substring Presence | Vultr Docs
December 5, 2024 - The str.contains() function in Python Panda Series offers a powerful yet straightforward method to check for the presence of substrings, allowing you to filter and manage data effectively.
🌐
DataScience Made Simple
datasciencemadesimple.com › home › get the substring of the column in pandas python
Get the substring of the column in pandas python - DataScience Made Simple
February 5, 2023 - str.slice function extracts the substring of the column in pandas dataframe python. Let’s see an Example of how to get a substring from column of pandas dataframe and store it in new column.
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › Series › str › replace
Python Pandas Series str replace() - Replace Substring | Vultr Docs
November 26, 2024 - import pandas as pd data = pd.Series(['foo', 'bar', 'baz', 'foobar']) modified_data = data.str.replace('foo', 'new') print(modified_data) Explain Code · This example replaces the substring 'foo' with 'new' in each element of the Series.
🌐
Saturn Cloud
saturncloud.io › blog › how-to-extract-substring-from-an-entire-column-in-pandas-dataframe
How to Extract Substring from an Entire Column in Pandas Dataframe | Saturn Cloud Blog
January 9, 2024 - AIn this blog, we'll delve into various techniques for extracting substrings from an entire column in a pandas dataframe. If you're a data scientist, you might encounter scenarios requiring the extraction of particular string components from a column. This could involve extracting the date or time from a timestamp column or isolating a specific segment from a string column.
🌐
Saturn Cloud
saturncloud.io › blog › how-to-filter-pandas-dataframe-by-substring-criteria
How to Filter Pandas DataFrame by Substring Criteria | Saturn Cloud Blog
January 9, 2024 - This code filters the DataFrame to include only rows where the emails column contains either the substring gmail.com or yahoo.com. ... Error 1: Incorrect Column Name: Description: If the column name is misspelled, Pandas won’t find the column, resulting in a KeyError.
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas filter dataframe by substring criteria
Pandas Filter DataFrame by Substring Criteria - Spark By {Examples}
March 27, 2024 - df2=df[df['Courses'].astype(str).str.contains("PySpark|Python")] # Filter rows that match a given string in a column by isin(). df2=df[df['Courses'].isin(["Spark"])] # Mix of upper and lowercase letters. print(df['Courses'].str.lower().isin(['Spark'])) # Using isin() to deal with two strings. df2=df[df['Courses'].isin(["Spark","Python"])] # Filter Pandas DataFrame of Multiple columns. df2=df.apply(lambda col: col.str.contains('Spark|Python', na=False), axis=1) # Join multiple terms. terms = ['Spark', 'PySpark'] df2=df[df['Courses'].str.contains('|'.join(terms))] # Using re.escape() function to get multiple columns. import re df2=df[df['Courses'].str.contains('|'.join(map(re.escape, terms)))] # Using IN operator to get substring of Pandas DataFrame.
🌐
Towards Data Science
towardsdatascience.com › home › latest › joining dataframes by substring match with python pandas
Joining DataFrames by substring match with Python Pandas | Towards Data Science
March 5, 2025 - Look that the value BC in partial_task_name is a substring of ABC and BCD, the expected result must produce many rows for this case, but how can we get many rows? The answer is using a Cartesian Product or Cross Join. To do a Cartesian Product in Pandas, do the following steps:
Top answer
1 of 3
2

This is perhaps more suited for StackOverflow. I would also use a better/more descriptive title for the question itself; that way others that are facing a similar problem are able to find it.

The reason you are seeing that error is because of the nan values, which are of type float. So while most of the rows in df['location'] contain strings, every row instance of an nan in the column is a float, and str.index() is not available for floats.

Your check of if df.location[i] == np.nan: is pointless, because np.nan == np.nan is always False due to the very definition of nan. Refer to this question on the topic. Because your check fails, the loop enters the else block and encounters a float object attempting to invoke a string method.

In my opinion you are using a very complicated approach to get what you want.

Replace your code with this. It should give you what you are looking for. Any nan values encountered will be handled by python.

df['location']=df['location'].str.replace(" ","").str.strip('(').str.strip(')')
df['latitude']=df['location'].str.split(',').str[0]
df['longitude']=df['location'].str.split(',').str[1]

I tested this using the following code segment:

df=pd.DataFrame()

df['location']=(
"(37.785719256680785, -122.40852313194863)",
"(37.78733980600732, -122.41063199757738)",
"(37.7946573324287, -122.42232562979227)",
"(37.79595867909168, -122.41557405519474)",
"(37.78315261897309, -122.40950883997789)",
np.nan,
"(37.78615261897309, -122.405550883997789)")

df['location']=df['location'].str.replace(" ", "").str.strip('(').str.strip(')')
df['latitude']=df['location'].str.split(',').str[0]
df['longitude']=df['location'].str.split(',').str[1]

print(df[['latitude','longitude']])

This produces the output:

             latitude             longitude
0  37.785719256680785   -122.40852313194863
1   37.78733980600732   -122.41063199757738
2    37.7946573324287   -122.42232562979227
3   37.79595867909168   -122.41557405519474
4   37.78315261897309   -122.40950883997789
5                 NaN                   NaN
6   37.78615261897309  -122.405550883997789
2 of 3
0

If I got it right from this one, the df['location'][i] should be some kind of a float and you can't get index from the float type. Check the type of the df['location'][i] with the isinstance(df['location'][i], float) you don't need to but just so you can see that it is really float. The error tells you everything. Maybe you are expecting string? There is not really much to say.