I wanted to rename only specific columns in my dataframe and the only method I know to do this using the method set_axis() which is used to rename columns or rows in the dataset. The example code below shows its usage:
The problem with this method is that I have to specify the names for all columns, even the ones which I don’t want to rename (column C in the above example). I believe there may be more efficient ways of doing this, if there are, please provide them with an example code.
@mubashir_rizvi, a simple way to rename specific columns in a data frame is to use the rename() function with a dictionary. The keys of the dictionary represent the original column names, and the values represent the new column names.
Hey @mubashir_rizvi , We can use the columns attribute and pass a list of columns to rename the columns of the data frame.
Note: The size of the columns list we pass should be equal to the number of columns in the data frame. If you don’t want to rename a column and keep it as it is, simply write the same name in the list.