oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

Renaming Columns #382

Open oldoc63 opened 2 years ago

oldoc63 commented 2 years ago

When we get our data from other sources, we often want to change the columns names. For example, we might want all of the column names to follow variable name rules, so that we can use df.column name (which tab-completes) rather than df['column_name'] (which takes up extra space).

You can change all of the column names at once by setting the .columns property to a different list. This is great when you need to change all of the columns names at once, but be careful! You can easily mislabel columns if you get the ordering wrong.

oldoc63 commented 2 years ago

You can also rename individual columns by using the .rename method. Pass a dictionary like the one below to the columns keyword argument:

oldoc63 commented 2 years ago

Using rename with only the colums keyword will create a new DataFrame, leaving your original DataFrame unchange. That's why we also passed in the keyword argument inplace=True. Using inplace=True lets us edit the original DataFrame.

There are several reasons why .rename is preferable to .columns: