Given a Pandas DataFrame, let’s see how to rename column names.
About Pandas DataFrame:
Pandas DataFrame are rectangular grids which are used to store data. It is easy to visualize and work with data when stored in dataFrame.
- It consists of rows and columns.
- Each row is a measurement of some instance while column is a vector which contains data for some specific attribute/variable.
- Each dataframe column has a homogeneous data throughout any specific column but dataframe rows can contain homogeneous or heterogeneous data throughout any specific row.
- Unlike two dimensional array, pandas dataframe axes are labeled.
Method #1: Using
One way of renaming the columns in a Pandas dataframe is by using the
rename() function. This method is quite useful when we need to rename some selected columns because we need to specify information only for the columns which are to be renamed.
Rename a single column.
Rename multiple column.
Method #2: By assigning a list of new column names
The columns can also be renamed by directly assigning a list containing the new names to the
columns attribute of the dataframe object for which we want to rename the columns. The disadvantage with this method is that we need to provide new names for all the columns even if want to rename only some of the columns.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Pandas Dataframe.rename()
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- How to select multiple columns in a pandas dataframe
- How to drop one or multiple columns in Pandas Dataframe
- Difference of two columns in Pandas dataframe
- Split a text column into two columns in Pandas DataFrame
- Change Data Type for one or more columns in Pandas Dataframe
- Getting frequency counts of a columns in Pandas DataFrame
- Dealing with Rows and Columns in Pandas DataFrame
- Iterating over rows and columns in Pandas DataFrame
- Split a String into columns using regex in pandas DataFrame
- Create a new column in Pandas DataFrame based on the existing columns
- Using dictionary to remap values in Pandas DataFrame columns
- Conditional operation on Pandas DataFrame columns
- Python | Pandas DataFrame.columns
- How to widen output display to see more columns in Pandas dataframe?
- Drop rows from Pandas dataframe with missing values or NaN in columns
- Loop or Iterate over all or certain columns of a dataframe in Python-Pandas
- Get the number of rows and number of columns in Pandas Dataframe
- How to create an empty DataFrame and append rows & columns to it in Pandas?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.