Using String Methods in Pandas. How to apply string methods to columns... | by Luay Matalka | Towards Data Science

Often in a pandas dataframe we have columns that contain string values. Luckily, pandas provides an easy way of applying string methods to whole columns which are just pandas series objects. We can access the values of these series objects (or columns) as strings and apply string methods to them by using the str attribute of the series. Let’s look at some examples here.
We will be using the ufo reports found here to create our dataframe. We then will be using the dataframe head method to look at the top 5 rows of our dataframe and the dataframe info method to inspect our column values.
The info method output tells us that all the non-null values in our columns are most likely string objects. We will mainly focus on the City column in this tutorial.
Remember that a pandas dataframe is made up of columns. Each of these columns is a pandas Series object. In pandas, there are methods and attributes of these dataframe and series objects that we can access. Pandas provides us with a number of Series methods designed to work on strings. These string methods will be applied to every single entry in your column.
To apply a string method to a column, we will be using the str attribute of that Series object. So in general, we will be using the following format:
Series.str.<function/property>
Let’s say that we want to convert all the city names in our ufo dataframe to uppercase. First we would need to access that series (or column), then add .str, and finally add the specific method we want to use. To find all the string methods that we have available, go here and locate the String handling section.
We can access the series or column using bracket or dot notation as follows:
# bracket notationufo[‘City’] # dot notationufo.City
Note how the type of the City column is a Series object. Therefore, we can apply any Series method to that object.
So to convert all the City names in our column to uppercase, we can add .str followed by the upper method as shown:
ufo.City.str.upper()
Note: This doesn’t actually change our original dataframe. If we want to change our original dataframe, we can assign this output value to our original City column with the following code:
ufo.City = ufo.City.str.upper()
Now the City column of our original ufo dataframe contains City names that are uppercase.
towardsdatascience.com
What if we want to replace all spaces in the City column with an underscore? We can accomplish this by using the replace method. We just specify what we want to replace and then what we want to replace it with.
ufo.City.str.replace(‘ ’, '_')
Note how all spaces have been replaced with an underscore.
We can also chain string methods together. Remember that these Series methods return a pandas Series object. So we can just add another string method to them. For example, let’s say that we want to replace all spaces with an underscore and change all the city names to lowercase. We can accomplish this with the following code:
ufo.City.str.replace(‘ ‘, ‘_’).str.lower()
Since ufo.City.str.replace(‘ ‘, ‘_’) returns a Series object, we can just use or chain another Series method to it.
towardsdatascience.com
In this tutorial, we learned how to access and apply string methods to columns in pandas dataframes.