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.
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)
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.
As shown in the output image, the least index of ‘e’ in series was returned and stored in new column.
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.
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.
- Python | pandas.map()
- Python | Pandas Dataframe.iat[ ]
- Python | Pandas Timestamp.dst
- Python | Pandas Series.at
- Python | Pandas dataframe.pow()
- Python | Pandas Index.max()
- Python | Pandas Timestamp.tz
- Python | Pandas DataFrame.ix[ ]
- Python | Pandas Index.min()
- Python | Pandas dataframe.mean()
- Python | Pandas Series.agg()
- Python | Pandas Index.all()
- Python | Pandas Index.any()
- Python | Pandas Panel.add()
- Python | Pandas Series.all()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.