• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Capitalize Column Names in a Dataframe

Capitalize Column Names in a Dataframe

Author: Aditya Raj
Last Updated: July 10, 2022

Pandas dataframes are used to handle tabular data in python. In this article, we will discuss different ways to capitalize column names in a dataframe in python.

Capitalize Column Names Using the str.upper() Method

The column names of a dataframe are stored in the ‘columns’ attribute. We can retrieve all the column names using the columns attribute as shown below.

import pandas as pd
df1 = pd.read_csv('student_details.csv')
print("The dataframe is:")
print(df1)
columns = df1.columns
print("The column names are:")
for name in columns:
    print(name)

Output:

The dataframe is:
     Name  Roll Number      Subject
0  Aditya           12       Python
1     Sam           23         Java
2   Chris           11          C++
3    Joel           10   JavaScript
4  Mayank            5   Typescript
The column names are:
Name
Roll Number
 Subject

As the column names are stored in a list,we can assign a new list containing the uppercase values of the original column names to capitalize the columns names of the dataframe. For this, we will first create an empty list say myList. After that, we will convert each column name to uppercase using the upper() method.

The upper() method, when invoked on a string, returns a new string with uppercase characters. We will obtain an uppercase string for each column name and store it in myList. After that, we will assign myList to the columns attribute. In this way, the uppercase strings will be assigned to the dataframe as the column names. You can observe this in the following example.

import pandas as pd

df1 = pd.read_csv('student_details.csv')
print("The dataframe is:")
print(df1)
columns = df1.columns
print("Originally, the column names are:")
for name in columns:
    print(name)
myList = []
for name in columns:
    myList.append(name.upper())
df1.columns = myList
columns = df1.columns
print("After capitalization, the column names are:")
for name in columns:
    print(name)

Output:

The dataframe is:
     Name  Roll Number      Subject
0  Aditya           12       Python
1     Sam           23         Java
2   Chris           11          C++
3    Joel           10   JavaScript
4  Mayank            5   Typescript
Originally, the column names are:
Name
Roll Number
 Subject
After capitalization, the column names are:
NAME
ROLL NUMBER
 SUBJECT

In the above example, instead of using the for loop, you can use list comprehension to capitalize the column names as shown below.

import pandas as pd

df1 = pd.read_csv('student_details.csv')
print("The dataframe is:")
print(df1)
columns = df1.columns
print("Originally, the column names are:")
for name in columns:
    print(name)
df1.columns = [x.upper() for x in columns]
columns = df1.columns
print("After capitalization, the column names are:")
for name in columns:
    print(name)

Output:

The dataframe is:
     Name  Roll Number      Subject
0  Aditya           12       Python
1     Sam           23         Java
2   Chris           11          C++
3    Joel           10   JavaScript
4  Mayank            5   Typescript
Originally, the column names are:
Name
Roll Number
 Subject
After capitalization, the column names are:
NAME
ROLL NUMBER
 SUBJECT

Capitalize Column Names Using series.str.upper() Method

Instead of using the upper() method defined for strings, we can use the Series.str.upper() method to capitalize the column names in a dataframe. To capitalize the column names, we can simply invoke the upper() method on the Index object in which the column names are stored. The Series.str.upper() when invoked on the dataframe.columns object, returns another object in which all the column names are capitalized. We can assign the object returned by the upper() method to the columns attribute of the dataframe to capitalize the column names as shown in the following example.

import pandas as pd

df1 = pd.read_csv('student_details.csv')
print("The dataframe is:")
print(df1)
columns = df1.columns
print("Originally, the column names are:")
for name in columns:
    print(name)
df1.columns = df1.columns.str.upper()
print("After capitalization, the column names are:")
columns = df1.columns
for name in columns:
    print(name)

Output:

The dataframe is:
     Name  Roll Number      Subject
0  Aditya           12       Python
1     Sam           23         Java
2   Chris           11          C++
3    Joel           10   JavaScript
4  Mayank            5   Typescript
Originally, the column names are:
Name
Roll Number
 Subject
After capitalization, the column names are:
NAME
ROLL NUMBER
 SUBJECT

Capitalize Column Names Using the rename() Method

We can also use the rename() method to capitalize the column names in a dataframe. For this, we can pass the the upper() method as an input argument to the columns parameter in the rename() method. After execution, the rename() method returns a dataframe with the capitalized column names. You can observe this in the following example.

import pandas as pd

df1 = pd.read_csv('student_details.csv')
print("The dataframe is:")
print(df1)
columns = df1.columns
print("Originally, the column names are:")
for name in columns:
    print(name)
newDf = df1.rename(columns=str.upper)
print("After capitalization, the column names are:")
columns = newDf.columns
for name in columns:
    print(name)

Output:

The dataframe is:
     Name  Roll Number      Subject
0  Aditya           12       Python
1     Sam           23         Java
2   Chris           11          C++
3    Joel           10   JavaScript
4  Mayank            5   Typescript
Originally, the column names are:
Name
Roll Number
 Subject
After capitalization, the column names are:
NAME
ROLL NUMBER
 SUBJECT

In this approach, the original dataframe is not modified. Instead, a new dataframe is created with capitalized column names. To modify the column names in the original dataframe, you can use the inplace parameter, and assign the value True to it in the rename() method. In this way, the original dataframe will be modified. You can observe this in the following example.

import pandas as pd

df1 = pd.read_csv('student_details.csv')
print("The dataframe is:")
print(df1)
columns = df1.columns
print("Originally, the column names are:")
for name in columns:
    print(name)
df1.rename(columns=str.upper, inplace=True)
print("After capitalization, the column names are:")
columns = df1.columns
for name in columns:
    print(name)

Output:

The dataframe is:
     Name  Roll Number      Subject
0  Aditya           12       Python
1     Sam           23         Java
2   Chris           11          C++
3    Joel           10   JavaScript
4  Mayank            5   Typescript
Originally, the column names are:
Name
Roll Number
 Subject
After capitalization, the column names are:
NAME
ROLL NUMBER
 SUBJECT

Conclusion

In this article, we have discussed different ways to capitalize column names in a dataframe in python. To know more about python programming, you can read this article on dictionary comprehension in python. You might also like this article on string concatenation in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us