Add column names to dataframe in Pandas
Sometimes, Pandas DataFrames are created without column names, or with generic default names (like 0, 1, 2, etc.). Let's learn how to add column names to DataFrames in Pandas.
Adding Column Names Directly to columns Attribute
The simplest way to add column names is by directly assigning a list of column names to the columns attribute of a DataFrame. This method is particularly useful when the DataFrame has already been created, but the column names must be updated.
import pandas as pd
# Creating DataFrame without column names
team = pd.DataFrame([["Amar", "Alpha", 23, 69],
["Barsha", "Bravo", 25, 54],
["Carlos", "Charlie", 22, 73],
["Tanmay", "Tango", 27, 70],
["Misbah", "Mike", 29, 74]])
print("Original DataFrame:")
display(team)
# Adding column names directly
team.columns = ['Name', 'Code', 'Age', 'Score']
print("\nAfter directly assigning to columns:")
display(team)
Output:
Renaming Columns Using rename() Method
The rename() method allows you to change specific column names without affecting the rest of the DataFrame. This is useful if you want to rename one or a few columns selectively.
# Renaming specific columns
team.rename(columns={'Name': 'Player', 'Code': 'Alias', 'Age': 'Years', 'Score': 'Points'}, inplace=True)
print(team)
Output:
Player Alias Years Points
0 Amar Alpha 23 69
1 Barsha Bravo 25 54
2 Carlos Charlie 22 73
3 Tanmay Tango 27 70
4 Misbah Mike 29 74
This method provides flexibility for renaming individual columns using a dictionary where the keys are current column names and the values are the new names.
Passing Column Names During DataFrame Creation
When creating a DataFrame, you can pass a list of column names directly in the columns
parameter. This is the easiest way to add column names at the time of DataFrame creation.
# Creating DataFrame with column names
team = pd.DataFrame([["Amar", "Alpha", 23, 69],
["Barsha", "Bravo", 25, 54],
["Carlos", "Charlie", 22, 73],
["Tanmay", "Tango", 27, 70],
["Misbah", "Mike", 29, 74]],
columns=['Name', 'Code', 'Age', 'Score'])
print(team)
Output:
Using set_axis() Method to Rename Columns of Pandas DataFrame
The set_axis() method is another option for renaming all columns of a DataFrame. This method allows you to specify column names in a list and apply them along a particular axis (axis=1 for columns). This can be helpful when you want to replace all the column names at once.
# Setting new column names using `set_axis()`
team = team.set_axis(['Player', 'Alias', 'Years', 'Points'], axis=1)
print(team)
Output:
Player Alias Years Points
0 Amar Alpha 23 69
1 Barsha Bravo 25 54
2 Carlos Charlie 22 73
3 Tanmay Tango 27 70
4 Misbah Mike 29 74
Adding New Columns with the assign() Method
While assign() is typically used for adding new columns to a DataFrame, you can also use it to add columns with meaningful names. This method returns a new DataFrame with the additional columns.
# Adding a new column with `assign()`
team = team.assign(Experience=[5, 3, 4, 6, 2])
print(team)
Output:
Player Alias Years Points Experience
0 Amar Alpha 23 69 5
1 Barsha Bravo 25 54 3
2 Carlos Charlie 22 73 4
3 Tanmay Tango 27 70 6
4 Misbah Mike 29 74 2
Modifying Column Names with add_prefix() and add_suffix()
If you want to add a prefix or suffix to every column name, you can use the add_prefix() and add_suffix() methods. These methods are helpful when you need to modify all column names in a consistent way.
# Adding a prefix to all column names
team = team.add_prefix('Team_')
display(team)
# Adding a suffix to all column names
team = team.add_suffix('_Info')
display(team)
Output: