Pandas

Rename Column in Pandas: 4 Different Ways

Manipulating your data in pandas becomes difficult if you have not properly set the columns name. Like for example if there is space between two words in a column name then it can lead to an access error. Therefore its necessary to change it. In this entire tutorial, I will show you different ways to rename a column in pandas. You have to just follow the steps or method given here.

Read a CSV File using Pandas

Before going to the method to rename a column in pandas lets first read a CSV file to demonstrate it. In pandas, there is a method for that and it is pandas.read_csv(). Lets read the CSV file.

Here I am using the car dataset. You can download the car dataset from here.

data = pd.read_csv("cars.csv")

After reading the CSV file you can know the columns of the dataset using data.columns.

print(data.columns)
Displaying the columns of the cars dataset

Rename columns in pandas with list

Renaming the columns through a list in pandas requires knowing the shape of the dataset you have. To know the shape of the data you have to use the shape() method of pandas.

After finding the shape of the dataset, now you will make a list of new columns’ names and pass them to the data. columns variable.

Like in our case, In this dataset, there are six columns. So I have to make a list of 6 column names and assign it to the dataset using the dot operator. Follow the code below.

column_name = ["N","M","C","D","H","D"]
data.columns=column_name

Now if you print the dataset then you will get the following output.

print(data.head())
Rename columns in pandas using list

Rename columns in pandas by position

You can rename columns in pandas using the position of the column also using the rename() method. But before it, you have to find the position of the column. Like in our example I want to rename the column name “mpg“. Then I have to find the column position or index. In our example, it is 1. So  I will pass the following things as an argument like below.

data.rename(columns={ data.columns[1]: "M" },inplace=True)

Now if you print the data you will get the following output.

print(data.head())
Rename columns in pandas by position

Rename one column in pandas.

If you want to rename only one column in pandas then you can do it using the rename() method. Choose the column you want to rename and pass the new column name. For example, I want to rename the column name “cyl” with CYL then I will use the following code.

data.rename(columns={"cyl":"CYL"},inplace=True)
print(data.head())

The output after renaming one column is below.

Rename one column in pandas

Rename multiple columns in pandas

You can rename multiple columns in pandas also using the rename() method. Just pass the names of columns as an argument inside the method. For example, I want to rename “cyl”, “disp” and “hp”, then I will use the following code.

data.rename(columns={"cyl":"CYL","disp":"DISP","hp":"HP"},inplace=True)
print(data.head())

You can get the following output after renaming the column names.

Rename multiple columns in pandas

Pandas rename columns by regex

There is a case when you have some character in the column name and you want to change or replace. Regex is used for it. Using it you can replace that character. Let’s make a pandas dataframe with a character “$” in each column name.

data = {"$a": [1,2,3,4],
            "$b": [10,20,30,40],
            "$c": [100,200,300,400]}
    df = pd.DataFrame(data)

The output of the above data is below.

Demo Pandas Dataframe

Now I want to remove “$” from each of the columns then I will use the replace() method for it. Use the code below.

df.columns = df.columns.str.replace(r"[$]", "")
print(df)

It will remove “$” from all of the columns. The final output will be like below.

Pandas rename columns by regex

Conclusion

There are many ways to Rename Column in Pandas. The above are the best methods We have found for you. These are generally used by other developers. One thing you will note that in many methods rename() method has been used.

Hope you have liked this tutorial. If you have any query regarding this then you can contact us for more information.

Source:

Official Pandas Documentation