![]() |
VOOZH | about |
Changing column names and row indexes in a Pandas DataFrame is a common task during data cleaning or formatting. For example, you may want to make the column names more descriptive or shift the row index labels for better readability. Let's explore different methods to efficiently change column names and row indexes in a Pandas DataFrame.
rename() function is one of the most flexible and widely used methods for changing specific column names and row indexes in a DataFrame. It allows you to pass dictionaries to the columns and index parameters, mapping old names or positions to new ones.
Col_1 Col_2 Row_1 Tom 15 Row_2 Nick 26 Row_3 John 17 Row_4 Peter 28
Explanation: This code creates a DataFrame df with columns "A" and "B". It then uses rename() to change column names to "Col_1" and "Col_2" and row indexes from 0–3 to "Row_1" through "Row_4".
This method replaces all column names or row indexes at once by assigning a new list to df.columns or df.index. It’s quick and simple but requires the new list to match the exact number of columns or rows.
Explanation: This code creates a copy of this DataFrame as res. It renames the columns to "Col_1" and "Col_2" using res.columns, and sets custom row labels "Row_1" to "Row_4" using res.index.
List comprehension allows renaming columns or indexes using a consistent pattern, like adding a prefix or suffix. It’s concise, flexible and ideal for dynamic or programmatic renaming, especially with large or auto-generated labels.
A_new B_new Index_0 Tom 15 Index_1 Nick 26 Index_2 John 17 Index_3 Peter 28
Explanation: This code copies the DataFrame to res, renames columns by appending "_new" (e.g., "A_new", "B_new") and updates row indexes to "Index_0" through "Index_3".
This method modifies a specific column name by directly accessing the underlying NumPy array using its index. It’s fast for renaming a single column but not ideal for regular use, as it bypasses safer, more maintainable methods.
A Student_Age 0 Tom 15 1 Nick 26 2 John 17 3 Peter 28
Explanation: This code copies df to res and renames the second column "B" to "Student_Age" using res.columns.values[1]. The final columns are "A" and "Student_Age".
Related articles: