In this section we will be using lower() function in pandas, to convert the character column of the python pandas dataframe to lowercase. We have listed some of different ways to convert string column to lower case in pandas
- If the input string is in any case (upper, lower or title) , lower() function in pandas converts the string to lower case.
- use str.lower() function to convert pandas column to lowercase
- use apply() function to convert pandas column to lowercase
- use apply() and lambda function to convert pandas string column to lowercase
- use map() function to convert pandas string column to lowercase
- Capitalize first character of each word in pandas column – using title() function
- Capitalize only first character of the entire sentence in pandas column – using capitalize() function
Lets look at these each case with an Example
Create dataframe:
## create dataframe import pandas as pd d = {'Quarters' : ['quarter1','quarter2','quarter3','quarter4'], 'Description' : ['First Quarter of the year', 'second quarter of the year', 'Third Quarter of the year', 'FOURTH QUARTER OF THE YEAR']} df=pd.DataFrame(d) df
resultant dataframe will be
Convert pandas column to lowercase : str.lower() function:
str.lower() function first converts a column into string column and then into lower case as shown below
### str.lower() function df['Description_Lower'] = df['Description'].str.lower() df
Convert pandas column to lowercase : apply() function:
within apply() function string function is used and it converts the pandas column to lower case as shown below
### apply() function df['Description_Lower'] = df['Description'].apply(str.lower) df
Convert pandas column to lowercase : apply() and lambda function:
within apply() function lambda function and string function is used to convert a column to lower case in pandas dataframe as shown below
### apply() function and lambda function df['Description_Lower'] = df['Description'].apply(lambda x: x.lower()) df
Convert pandas column to lowercase : using map() function:
within map() function str.lower() function is used to convert a column to lower case in pandas dataframe as shown below
### map() function df['Description_Lower'] = df['Description'].map(str.lower) df
Convert pandas column to Title case : Capitalize first character of each word of pandas column
we can use str.title() to capitalize the first character of each word in the column, i.e. to convert a column to title case in pandas dataframe as shown below
# Title function in python to convert the character column to Title case df['Description_Title'] = df['Description'].str.title() df
so the output will be
Convert pandas column to Caplitalize case : Capitalize first character of entire string of the pandas column
we can use str.capitalize() to capitalize the only first character of entire sentence in the column, i.e. to convert a column to capitalize case in pandas dataframe as shown below
# capitalize function in python to convert the character column to capitalize case df['Description_Capitalize'] = df['Description'].str.capitalize() df
so the output will have only first character of entire sentence is capitalized