Cleaning column names

Hello everyone! I am currently working on project 3 “Exploring Ebay Car Sales Data”, in particular on step 2 “Cleaning column names”. I am using this kind of code to rename each column:

copy = copy.str.replace(‘monthOfRegistration’, ‘registration_month’)
copy = copy.str.replace(‘notRepairedDamage’, ‘unrepaired_damage’)

and etc.

It is working, but I wanted to ask, is there any way to optimise this process and make it faster or in the real life we also have to rename each column on-by-one (the process seems a bit boring and time-consuming if there are a lot of columns)?

I came up only with this one to rename all the Type to _type:

copy = copy.str.replace(‘T’,’_t’)

Thank you in advance for your response.

1 Like

Hi Aleksei,

Welcome to the Community!

You can rename as many columns as you want using the following syntax:

df.rename(columns={"old": "new", "old2": "new2", "old3": "new3"})

Hi Elena,

Thank you, it is a pleasure to be a part of the Community!

Thank you for your answer. This form seems a bit more compact. Besides, I can create a dictionary with old-new names and assign them to the variable, then just use that variable inside df.rename method as I understood.

1 Like

Hi Aleksei,

Yes, you’re right, creating first this dictionary and then putting it in df.rename() is even a better approach since it makes the code more clean and readable. Also, it’s the best solution when you have quite a lot of columns to be renamed.

1 Like