You may need to make some small adjustments to your Pandas data once it has been created, such as its labels. This can be done with built-in solutions. Continue reading to find out how to rename columns with list in Pandas.
How To Rename Columns With List In Pandas
Recreating The DataFrame
Here is an example that creates a DataFrame containing information about several websites.
We have created a DataFrame using two lists, one for its columns and one for its data. Read this guide if you want to know more methods of creating DataFrames.
Now let’s say you want to change every label into uppercase. First, you will need to decide whether to capitalize the label names manually or automatically.
We recommend using the method upper(), while you totally can type in the names yourself. These two statements should produce the same result:
new_headers = [f.upper() for f in headers] new_headers = ['SITE', 'RANKING', 'TYPE', 'TOP LANGUAGE']
To replace the old column names with the new ones, you can recreate the DataFrame with the new list of headers:
However, this approach only works when the original DataFrame is created from two separate objects representing its columns and data. It isn’t viable when you, for example, import everything from a single CSV file. Splitting the data from its columns is not a practical idea.
The two options below can come in handy in those situations. You can make use of them in a broader sense: when you want to rename column names without a separate representation of the data.
The attribute .columns returns the column labels of a Pandas DataFrame. You can access it through this syntax:
In which dataframe is the name of your DataFrame.
You can assign the list containing new labels to this attribute. This step will rename every column name in the DataFrame to the items in that list.
For example, to rename the DataFrame in our example:
As you can see, the assignment operator introduces changes to the attribute immediately. And the DataFrame has new column names as a result.
The attribute .columns gives us a convenient tool to replace column names of a DataFrame. But you can achieve this task through another tool: the method set_axis().
This option can do more than just renaming your DataFrame’s columns. It has a more general purpose, which is to assign an index to any given axis of a Pandas DataFrame.
Its syntax is as follows:
DataFrame.set_axis(labels, axis, inplace)
- axis: this parameter determines which axis will have the new index. Like other functions and methods in Pandas, the value 1 indicates columns and 0 rows. The default value is 0.
- labels: the new index for the said axis. It should be a list-like object.
- inplace: this boolean parameter controls whether the method alters the current DataFrame or makes changes in a copied DataFrame and returns it. The default is False.
This statement will create a new DataFrame and change its column labels:
Set inplace to True if you want to change the original DataFrame instead:
df.set_axis(new_headers, axis = 1, inplace=True)
You can rename columns with list in Pandas with either the attribute columns or the method set_axis(). They can work on the fly and set the desired index without the need to create a new DataFrame.