This article will discuss different ways to convert all values of a Pandas Dataframe column to uppercase in Python.
Table of Contents
- Convert column values to uppercase using str.upper()
- Convert column values to uppercase using apply()
- Convert column values to uppercase 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':['sanjay','atharv','ritika','sravan','harsh'], 'Age' :[31, 33, 32, 21, 33], 'Subjects':['maths','physics','chemistry','python', 'java']}) # Display the Dataframe print(df)
Output:
Roll_Number Name Age Subjects 0 11 sanjay 31 maths 1 12 atharv 33 physics 2 13 ritika 32 chemistry 3 14 sravan 21 python 4 15 harsh 33 java
Convert column values to uppercase using str.upper()
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 upper() function to convert all the values in that series (dataframe column) to uppercase. The syntax is as follows,
df['column_name'].str.upper()
where, df is the input dataframe and column_name is the name of the dataframe column, whose values need to be converted into uppercase.
Example: In this example, we are going to convert values of ‘Name’ and ‘ Subjects ‘ columns values into uppercase.
import pandas as pd # create dataframe with 5 rows and 4 columns df= pd.DataFrame({'Roll_Number':[11 ,12, 13, 14, 15], 'Name':['sanjay','atharv','ritika','sravan','harsh'], 'Age' :[31, 33, 32, 21, 33], 'Subjects':['maths','physics','chemistry','python', 'java']}) # Display the Dataframe print(df) # Convert the value of 'Name' column to uppercase df['Name'] = df['Name'].str.upper() # Convert the value of 'Subjects' column to uppercase df['Subjects'] = df['Subjects'].str.upper() # Display the Dataframe print(df)
Output:
Roll_Number Name Age Subjects 0 11 sanjay 31 maths 1 12 atharv 33 physics 2 13 ritika 32 chemistry 3 14 sravan 21 python 4 15 harsh 33 java Roll_Number Name Age Subjects 0 11 SANJAY 31 MATHS 1 12 ATHARV 33 PHYSICS 2 13 RITIKA 32 CHEMISTRY 3 14 SRAVAN 21 PYTHON 4 15 HARSH 33 JAVA
Convert column values to uppercase 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.
Now to convert all values in selected column (series), pass the str.upper() function as argument to the apply() function. It will convert all values in column to upper 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':['sanjay','atharv','ritika','sravan','harsh'], 'Age' :[31, 33, 32, 21, 33], 'Subjects':['maths','physics','chemistry','python', 'java']}) # Display the Dataframe print(df) # Convert the value of 'Name' column to uppercase df['Name'] = df['Name'].apply(str.upper) # Convert the value of 'Subjects' column to uppercase df['Subjects'] = df['Subjects'].apply(str.upper) # Display the Dataframe print(df)
Output:
Roll_Number Name Age Subjects 0 11 sanjay 31 maths 1 12 atharv 33 physics 2 13 ritika 32 chemistry 3 14 sravan 21 python 4 15 harsh 33 java Roll_Number Name Age Subjects 0 11 SANJAY 31 MATHS 1 12 ATHARV 33 PHYSICS 2 13 RITIKA 32 CHEMISTRY 3 14 SRAVAN 21 PYTHON 4 15 HARSH 33 JAVA
Convert column values to uppercase 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.upper() function as argument to the map() function. It will convert all values in the column to upper 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':['sanjay','atharv','ritika','sravan','harsh'], 'Age' :[31, 33, 32, 21, 33], 'Subjects':['maths','physics','chemistry','python', 'java']}) # Display the Dataframe print(df) # Convert the value of 'Name' column to uppercase df['Name'] = df['Name'].map(str.upper) # Convert the value of 'Subjects' column to uppercase df['Subjects'] = df['Subjects'].map(str.upper) # Display the Dataframe print(df)
Output:
Roll_Number Name Age Subjects 0 11 sanjay 31 maths 1 12 atharv 33 physics 2 13 ritika 32 chemistry 3 14 sravan 21 python 4 15 harsh 33 java Roll_Number Name Age Subjects 0 11 SANJAY 31 MATHS 1 12 ATHARV 33 PHYSICS 2 13 RITIKA 32 CHEMISTRY 3 14 SRAVAN 21 PYTHON 4 15 HARSH 33 JAVA
Summary
In this article, we learn about three different ways to convert column values to uppercase in a Pandas dataframe.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.