Open In App

Python | Pandas Series.str.lower(), upper() and title()

Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier. Python has some inbuilt methods to convert a string into a lower, upper, or Camel case. But these methods don’t work on lists and other multi-string objects.

Pandas is a library for Data analysis that provides separate methods to convert all values in a series to respective text cases. Since, lower, upper, and title are Python keywords too, .str has to be prefixed before calling these functions on a Pandas series.

Syntax:
Series.str.lower()
Series.str.upper()
Series.str.title()

Parameters: Doesn’t take any parameter

Return Type:
Series with new values

Dataset:

To download the CSV file used, Click Here. In the example we have took, the data frame used contains data of some employees. Lets load the data and take a look at it.




# importing pandas package
import pandas as pd
 
# making data frame from csv file
data = pd.read_csv("employees.csv")
print("Example Dataset:")
print(data.head())

Output:

Example Dataset:
  First Name  Gender Start Date Last Login Time  Salary  Bonus %  Senior Management             Team 
0    Douglas    Male   8/6/1993        12:42 PM   97308    6.945               True        Marketing   
1     Thomas    Male  3/31/1996         6:53 AM   61933    4.170               True              NaN  
2      Maria  Female  4/23/1993        11:17 AM  130590   11.858              False          Finance 
3      Jerry    Male   3/4/2005         1:00 PM  138705    9.340               True          Finance   
4      Larry    Male  1/24/1998         4:47 PM  101004    1.389               True  Client Services 

Using .lower() on a Series

In this example, .lower() function is being called by the First Name column and hence, all the values in the First name column will be converted in to lower case.




# converting and overwriting values in column
data["First Name"]= data["First Name"].str.lower()
 
# display
print(data.head())

Output:

  First Name  Gender Start Date Last Login Time  Salary  Bonus %  Senior Management             Team 
0    douglas    Male   8/6/1993        12:42 PM   97308    6.945               True        Marketing   
1     thomas    Male  3/31/1996         6:53 AM   61933    4.170               True              NaN  
2      maria  Female  4/23/1993        11:17 AM  130590   11.858              False          Finance 
3      jerry    Male   3/4/2005         1:00 PM  138705    9.340               True          Finance   
4      larry    Male  1/24/1998         4:47 PM  101004    1.389               True  Client Services  

As shown in the output of data frame, all values in the First name column have been converted into lower case.

Using .upper() on a Series

In this example, .upper() function is being called by the Team column and hence all the values in the Team column will be converted into upper case.




# converting and overwriting values in column
data["Team"]= data["Team"].str.upper()
 
# display
print(data.head())

Output:

  First Name  Gender Start Date Last Login Time  Salary  Bonus %  Senior Management             Team 
0    Douglas    Male   8/6/1993        12:42 PM   97308    6.945               True        MARKETING   
1     Thomas    Male  3/31/1996         6:53 AM   61933    4.170               True              NaN  
2      Maria  Female  4/23/1993        11:17 AM  130590   11.858              False          FIANACE 
3      Jerry    Male   3/4/2005         1:00 PM  138705    9.340               True          FINANCE   
4      Larry    Male  1/24/1998         4:47 PM  101004    1.389               True  CLIENT SERVICES

As shown in the output of data frame, all values in the Team column have been converted into upper case.

Using .title() on a Series

In this example, .title() function is being called by the Team column and hence, all the values in the into column will be converted in to Camel case. Since the values in the Team column were already in camel case, it has been converted to Upper case before and then again to camel case in order to verify the functionality of .title() method.




# converting and overwriting values in column
data["Team"]= data["Team"].str.upper().str.title()
 
# display
print(data.head())

Output:

  First Name  Gender Start Date Last Login Time  Salary  Bonus %  Senior Management             Team 
0    Douglas    Male   8/6/1993        12:42 PM   97308    6.945               True        Marketing   
1     Thomas    Male  3/31/1996         6:53 AM   61933    4.170               True              NaN  
2      Maria  Female  4/23/1993        11:17 AM  130590   11.858              False          Finance 
3      Jerry    Male   3/4/2005         1:00 PM  138705    9.340               True          Finance   
4      Larry    Male  1/24/1998         4:47 PM  101004    1.389               True  Client Services 

As shown in the output of data frame, all values in the Team column have been converted into Camel case.


Article Tags :
Uncategorized