Python | Pandas Series.str.index()
Last Updated :
13 Jul, 2020
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages.
Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas
str.index()
method is used to search and return lowest index of a substring in particular section (Between start and end) of every string in a series. This method works in a similar way to
str.find() but on not found case, instead of returning -1, str.index() gives a ValueError.
Syntax: Series.str.index(sub, start=0, end=None)
Parameters:
sub: String or character to be searched in the text value in series
start: String or character to be searched in the text value in series
end: String or character to be searched in the text value in series
Return type: Series with least index of substring if found.
To download the data set used in following example, click
here.
In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.
Example #1: Finding index when substring exists in every string
In this example, 'e' is passed as substring. Since 'e' exists in all 5 strings, least index of it's occurrence is returned. Before applying any operations, null rows were removed using .dropna() method.
Python3 1==
# importing pandas module
import pandas as pd
# reading csv file from url
data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv")
# dropping null value columns to avoid errors
data.dropna(inplace = True)
# extracting 5 rows
short_data = data.head().copy()
# calling str.index() method
short_data["Index Name"]= short_data["Name"].str.index("e")
# display
short_data
Output:
As shown in the output image, the least index of 'e' in series was returned and stored in new column.
Example #2:
In this example, 'a' is searched in top 5 rows. Since 'a' doesn't exist in every string, value error will be returned. To handle error, try and except is used.
Python3 1==
# importing pandas module
import pandas as pd
# reading csv file from url
data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv")
# dropping null value columns to avoid errors
data.dropna(inplace = True)
# extracting 5 rows
short_data = data.head().copy()
# calling str.index() method
try:
short_data["Index Name"]= short_data["Name"].str.index("a")
except Exception as err:
print(err)
# display
short_data
Output:
As shown in output image, the output data frame is not having the Index Name column and the error "substring not found" was printed. That is because str.index() returns valueError on not found and hence it must have gone to except case and printed the error.
Similar Reads
Python | Pandas Index.to_series() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.to_series() function create a Series with both index and values equal to
2 min read
Python | Pandas Series.sort_index() Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.sort_index() function is used
2 min read
Python | Pandas Series.str.encode() Series.str can be used to access the values of the series as strings and apply several methods to it. Pandas Series.str.encode() function is used to encode character string in the Series/Index using indicated encoding. Equivalent to str.encode(). Syntax: Series.str.encode(encoding, errors='strict')
2 min read
Python | Pandas Series.str.decode() Series.str can be used to access the values of the series as strings and apply several methods to it. Pandas Series.str.decode() function is used to decode character string in the Series/Index using indicated encoding. This function is equivalent to str.decode() in python2 and bytes.decode() in pyth
2 min read
Python | Pandas Series.str.center() Series.str can be used to access the values of the series as strings and apply several methods to it. Pandas Series.str.center() function is used for filling left and right side of strings in the Series/Index with an additional character. The function is equivalent to Python's str.center(). Syntax:
3 min read
Python | Pandas Series.ix Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas series is a One-dimensional ndarray with axis labels. The labels need not be un
2 min read
Sort a Pandas Series in Python Series is a one-dimensional labeled array capable of holding data of the type integer, string, float, python objects, etc. The axis labels are collectively called index. Now, Let's see a program to sort a Pandas Series. For sorting a pandas series the Series.sort_values() method is used. Syntax: Se
3 min read
Python | Pandas Series.str.extract() Series.str can be used to access the values of the series as strings and apply several methods to it. Pandas Series.str.extract() function is used to extract capture groups in the regex pat as columns in a DataFrame. For each subject string in the Series, extract groups from the first match of regul
4 min read
Python | Pandas Series.set_axis() Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.set_axis() function is used t
2 min read
Python | Pandas Index.size Pandas Index is an immutable ndarray implementing an ordered, sliceable set. It is the basic object which stores the axis labels for all pandas objects. Pandas Index.size attribute return the number of elements in the underlying data of the given Index object. Syntax: Index.size Parameter : None Ret
2 min read