In this tutorial we will learn how to get the index or position of substring in a column of a dataframe in python – pandas.
We will be using find() function to get the position of substring in python.
Syntax of Find function:
Example of indexing a substring in a column:
Create a dataframe:
#create dataframe import pandas as pd d = {'Quarters' : ['quarter1 Revenue','quarter2 Revenue','quarter3 Revenue','quarter4 Revenue'], 'Revenue':[23400344.567,54363744.678,56789117.456,4132454.987]} df=pd.DataFrame(d) print df
Resultant dataframe will be
Indexing a substring of a column in dataframe Example:
# Index of a substring of dataframe in Python df['Index'] = map(lambda x: x.find('3 Rev'), df['Quarters']) print df
With the help of find() function we will be finding the position of substring “3 Rev” in Quarters column of df dataframe and storing it in a Index column.
When substring is found its starting position in returned
When substring is not found then -1 is returned. So the resultant data frame will be
Indexing a substring of a column in dataframe with beg and end:
# Index of a substring of dataframe in Python with begining and end df['Index'] = map(lambda x: x.find('quar',0,5), df['Quarters']) print df
With the help of find() function we will be finding the position of substring “quar” with beg and end parameters as 0 and 5 in Quarters column of df dataframe and storing it in a Index column.
When substring is found its starting position in returned
When substring is not found then -1 is returned. So the resultant data frame will be
Here “quar” substring is found in all the rows of Quarters column at the position 0, so 0 is returned for all the rows