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.
The post Capitalize Column Names in a Dataframe appeared first on PythonForBeginners.com.