How to rename the columns in DataFrame using Pandas

In this shot, we will discuss how to change the column names of a DataFrame in Python using pandas.

Let’s first create a DataFrame.

import pandas as pd
df = pd.DataFrame({
'col one':[100, 200],
'col two':[300, 400]
})
print(df)

Explanation

  • In line 1, we import the required package.
  • From lines 3 to 6, we create a simple DataFrame df containing two rows and two columns.
  • In line 8, we print the DataFrame.

To access the column of a DataFrame, one approach is to use dot notation (df.column_name). However, this approach doesn’t work here, as our column names have spaces. So, we first rename the columns using the rename() function.

df = df.rename({
'col one':'col_one'
}, axis='columns')
print("Replacing only the first column: \n", df)
df.columns = ['col_one', 'col_two']
print("\nReplacing all the columns: \n", df)

Explanation

  • In line 1, we use the rename() function and pass in the old column name and the new column name. This changes col one to col_one.
  • In line 4, we print the new DataFrame with a new column name.
  • In line 6, we specify new column names and assign them to the columns property of the DataFrame. This changes all of the column names.
  • In line 7, we print the new DataFrame with its new column names. Here, you can see that all column names are now changed.

The process of how to only change some of the characters in the column names is shown below.

print("Original DataFrame: \n", df)
df.columns = df.columns.str.replace(' ', '_')
print("New DataFrame:\n", df)

Explanation

  • In line 1, we print our original DataFrame.
  • In line 3, we change all the spaces in the names of the columns to underscores (_).
  • In line 4, we print the new DataFrame with its new column names.

Suppose we only want to add a prefix or suffix in all the column names. We can use the add_prefix() and add_suffix() methods to do this.

print("Original DataFrame: \n", df)
df = df.add_prefix('X_')
print("New DataFrame after Prefix:\n", df)
df = df.add_suffix('_Y')
print("New DataFrame after Suffix:\n", df)

Explanation

  • In line 1, we print the original DataFrame.
  • In line 3, we add a prefix to all the column names.
  • In line 4, we print the new DataFrame with its new column names.
  • In line 6, we add a suffix to all the column names.
  • In line 7, we print the new DataFrame with its new column names.

Free Resources