This article will discuss different ways to convert all values of a Pandas Dataframe column to lowercase in Python.
Table of Contents
- Convert column values to lowercase using str.lower()
- Convert column values to lowercase using apply()
- Convert column values to lowercase using map()
A DataFrame is a data structure that stores the data in rows and columns. We can create a DataFrame using pandas.DataFrame() method. Let’s create a dataframe with five rows and four columns,
import pandas as pd # create dataframe with 5 rows and 4 columns df= pd.DataFrame({'Roll_Number':[11 ,12, 13, 14, 15], 'Name':['MARK','JOHN','RITIKA','SRAVAN','HARSHA'], 'Age' :[21, 23, 22, 21, 23], 'Subjects':['PHP','JAVA','CPP','PYTHON', 'HTML']}) # Display the Dataframe print(df)
Output:
Roll_Number Name Age Subjects 0 11 MARK 21 PHP 1 12 JOHN 23 JAVA 2 13 RITIKA 22 CPP 3 14 SRAVAN 21 PYTHON 4 15 HARSHA 23 HTML
Convert column values to lowercase using str.lower()
Select the column from Dataframe as a Series object using indexing. Then get hold of the underlying string object from the Series object and call the lower() function to convert all the values in that series (dataframe column) to lowercase. The syntax is as follows,
Frequently Asked:
df['column_name'].str.lower()
where, df is the input dataframe and column_name is the name of the dataframe column, whose values need to be converted into lowercase.
Example: In this example, we are going to convert values of ‘Name’ and ‘ Subjects ‘ columns values into lowercase.
import pandas as pd # create dataframe with 5 rows and 4 columns df= pd.DataFrame({'Roll_Number':[11 ,12, 13, 14, 15], 'Name':['MARK','JOHN','RITIKA','SRAVAN','HARSHA'], 'Age' :[21, 23, 22, 21, 23], 'Subjects':['PHP','JAVA','CPP','PYTHON', 'HTML']}) # Display the Dataframe print(df) # Convert the value of 'Name' column to lowercase df['Name'] = df['Name'].str.lower() # Convert the value of 'Subjects' column to lowercase df['Subjects'] = df['Subjects'].str.lower() # Display the Dataframe print(df)
Output:
Roll_Number Name Age Subjects 0 11 MARK 21 PHP 1 12 JOHN 23 JAVA 2 13 RITIKA 22 CPP 3 14 SRAVAN 21 PYTHON 4 15 HARSHA 23 HTML Roll_Number Name Age Subjects 0 11 mark 21 php 1 12 john 23 java 2 13 ritika 22 cpp 3 14 sravan 21 python 4 15 harsha 23 html
Convert column values to lowercase using apply()
Using column name, select a column of Dataframe as a Series object and call the apply() function on that Series object. In the apply() function, pass a function as an argument. The apply() function will call the supplied function for each value in the Series i.e., the Dataframe column.
Latest Python - Video Tutorial
Now to convert all values in selected column (series), pass the str.lower() function as argument to the apply() function. It will convert all values in column to lower case. Checkout the complete example as follows,
import pandas as pd # create dataframe with 5 rows and 4 columns df= pd.DataFrame({'Roll_Number':[11 ,12, 13, 14, 15], 'Name':['MARK','JOHN','RITIKA','SRAVAN','HARSHA'], 'Age' :[21, 23, 22, 21, 23], 'Subjects':['PHP','JAVA','CPP','PYTHON', 'HTML']}) # Display the Dataframe print(df) # Convert the value of 'Name' column to lowercase df['Name'] = df['Name'].apply(str.lower) # Convert the value of 'Subjects' column to lowercase df['Subjects'] = df['Subjects'].apply(str.lower) # Display the Dataframe print(df)
Output:
Roll_Number Name Age Subjects 0 11 MARK 21 PHP 1 12 JOHN 23 JAVA 2 13 RITIKA 22 CPP 3 14 SRAVAN 21 PYTHON 4 15 HARSHA 23 HTML Roll_Number Name Age Subjects 0 11 mark 21 php 1 12 john 23 java 2 13 ritika 22 cpp 3 14 sravan 21 python 4 15 harsha 23 html
Convert column values to lowercase using map()
Using column name, select a column of Dataframe as a Series object and call the map() function on that Series object. In the map() function, pass a function as an argument. The map() function will call the supplied function on each value in the Series i.e. the Dataframe column.
Now to convert all values in selected column (series), pass the str.lower() function as argument to the map() function. It will convert all values in the column to lower case. Check out the complete example as follows,
import pandas as pd # create dataframe with 5 rows and 4 columns df= pd.DataFrame({'Roll_Number':[11 ,12, 13, 14, 15], 'Name':['MARK','JOHN','RITIKA','SRAVAN','HARSHA'], 'Age' :[21, 23, 22, 21, 23], 'Subjects':['PHP','JAVA','CPP','PYTHON', 'HTML']}) # Display the Dataframe print(df) # Convert the value of 'Name' column to lowercase df['Name'] = df['Name'].map(str.lower) # Convert the value of 'Subjects' column to lowercase df['Subjects'] = df['Subjects'].map(str.lower) # Display the Dataframe print(df)
Output:
Roll_Number Name Age Subjects 0 11 MARK 21 PHP 1 12 JOHN 23 JAVA 2 13 RITIKA 22 CPP 3 14 SRAVAN 21 PYTHON 4 15 HARSHA 23 HTML Roll_Number Name Age Subjects 0 11 mark 21 php 1 12 john 23 java 2 13 ritika 22 cpp 3 14 sravan 21 python 4 15 harsha 23 html
Summary
In this article, we learn about three different ways to convert column values to lowercase in a Pandas dataframe.
Latest Video Tutorials