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 pddf = pd.DataFrame({'col one':[100, 200],'col two':[300, 400]})print(df)
Explanation
df
containing two rows and two columns.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
rename()
function and pass in the old column name and the new column name. This changes col one
to col_one
.columns
property of the DataFrame. This changes all of the column names.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
_
).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