. Advertisement .
..3..
. Advertisement .
..4..
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.
import pandas as pd
headers = ['Site', 'Ranking', 'Type', 'Top Language']
data = [
['ITTutoria', 1, 'Tutorials', 'Python'],
['Stack Overflow', 2, 'Q&A', 'JavaScript'],
['Quora', 3, 'Q&A', None],
['Reddit', 4, 'Forum', None]
]
df = pd.DataFrame(data, columns = headers)
df
Output:
Site Ranking Type Top Language
0 ITTutoria 1 Tutorials Python
1 Stack Overflow 2 Q&A JavaScript
2 Quora 3 Q&A None
3 Reddit 4 Forum None
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:
>>> df = pd.DataFrame(data, columns = new_headers)
>>> df
SITE RANKING TYPE TOP LANGUAGE
0 ITTutoria 1 Tutorials Python
1 Stack Overflow 2 Q&A JavaScript
2 Quora 3 Q&A None
3 Reddit 4 Forum None
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.
Using .columns
The attribute .columns returns the column labels of a Pandas DataFrame. You can access it through this syntax:
dataframe.columns
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:
>>> df.columns = new_headers
>>> df.columns
Index(['SITE', 'RANKING', 'TYPE', 'TOP LANGUAGE'], dtype='object')
>>> df
SITE RANKING TYPE TOP LANGUAGE
0 ITTutoria 1 Tutorials Python
1 Stack Overflow 2 Q&A JavaScript
2 Quora 3 Q&A None
3 Reddit 4 Forum None
As you can see, the assignment operator introduces changes to the attribute immediately. And the DataFrame has new column names as a result.
Using DataFrame.set_axis()
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)
Where:
- 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:
>>> df.set_axis(new_headers, axis = 1)
SITE RANKING TYPE TOP LANGUAGE
0 ITTutoria 1 Tutorials Python
1 Stack Overflow 2 Q&A JavaScript
2 Quora 3 Q&A None
3 Reddit 4 Forum None
Set inplace to True if you want to change the original DataFrame instead:
df.set_axis(new_headers, axis = 1, inplace=True)
Conclusion
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.
Leave a comment