
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Apply uppercase to a column in Pandas dataframe in Python
In this tutorial, we are going to see how to make a column of names to uppercase in DataFrame. Let's see different ways to achieve our goal.
Example
We can assign a column to the DataFrame by making it uppercase using the upper() method.
Let's see the code.
# importing the pandas package import pandas as pd # data for DataFrame data = { 'Name': ['Hafeez', 'Aslan', 'Kareem'], 'Age': [19, 21, 18], 'Profession': ['Developer', 'Engineer', 'Artist'] } # creating DataFrame data_frame = pd.DataFrame(data) # displaying the DataFrame print('---------------------Before-------------------') print(data_frame) print() # making the Name column strings to upper case data_frame['Name'] = data_frame['Name'].str.upper() # displaying the DataFrame print('---------------------After-------------------') print(data_frame)
Output
If you run the above program, you will get the following results.
---------------------Before------------------- Name Age Profession 0 Hafeez 19 Developer 1 Aslan 21 Engineer 2 Kareem 18 Artist ---------------------After------------------- Name Age Profession 0 HAFEEZ 19 Developer 1 ASLAN 21 Engineer 2 KAREEM 18 Artist
Example
We can also achieve the same thing using apply() method of DataFrame. Let's see the related code.
# importing the pandas package import pandas as pd # data for DataFrame data = { 'Name': ['Hafeez', 'Aslan', 'Kareem'], 'Age': [19, 21, 18], 'Profession': ['Developer', 'Engineer', 'Artist'] } # creating DataFrame data_frame = pd.DataFrame(data) # displaying the DataFrame print('---------------------Before-------------------') print(data_frame) print() # making the Name column strings to upper case data_frame['Name'] = data_frame['Name'].apply(lambda name : name.upper()) # displaying the DataFrame print('---------------------After-------------------') print(data_frame)
Output
If you run the above program, you will get the following results.
---------------------Before------------------- Name Age Profession 0 Hafeez 19 Developer 1 Aslan 21 Engineer 2 Kareem 18 Artist ---------------------After------------------- Name Age Profession 0 HAFEEZ 19 Developer 1 ASLAN 21 Engineer 2 KAREEM 18 Artist
Conclusion
I hope you learned something from the tutorial. If you have any doubts regarding the tutorial, ask them in the comment section.
- Related Articles
- Apply uppercase to a column in Pandas dataframe
- Apply function to every row in a Pandas DataFrame in Python
- Apply function to every row in a Pandas DataFrame
- Python – Create a new column in a Pandas dataframe
- Python - Add a zero column to Pandas DataFrame
- Adding a new column to existing DataFrame in Pandas in Python
- Python - Add a prefix to column names in a Pandas DataFrame
- Adding a new column to an existing DataFrame in Python Pandas
- Python - Move a column to the first position in Pandas DataFrame?
- Python - Stacking a multi-level column in a Pandas DataFrame
- Python - How to select a column from a Pandas DataFrame
- Python Pandas – Display all the column names in a DataFrame
- Python Pandas – Remove numbers from string in a DataFrame column
- Python - Calculate the variance of a column in a Pandas DataFrame
- How to shift a column in a Pandas DataFrame?

Advertisements