Now, we’ll see how we can get the substring for all the values of a column in a Pandas dataframe. This extraction can be very useful when working with data. For example, we have the first name and last name of different people in a column and we need to extract the first 3 letters of their name to create their username.
We can loop through the range of the column and calculate the substring for each value in the column.
Note: For more information, refer Python Extracting Rows Using Pandas
Example 2: In this example we’ll use
Example 3: We can also use the str accessor in a different way by using square brackets.
Example 4: We can also use str.extract for this task. In this example we’ll store last name of each person in “LastName” column.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course