Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Rename column names by index in a Pandas DataFrame without using rename()
Sometimes you need to rename column names in a Pandas DataFrame by their position (index) rather than by name. Python provides a simple way to do this by directly modifying the columns.values array without using the rename() method.
Using columns.values with Index
You can rename columns by accessing their position in the columns.values array. This approach is useful when you know the column positions but not necessarily their current names.
Example
Let's create a DataFrame and rename all columns by their index positions ?
import pandas as pd
# Create DataFrame
dataFrame = pd.DataFrame(
{
"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000],
"Units": [90, 120, 100, 150, 200, 130]
}
)
print("Original DataFrame:")
print(dataFrame)
# Renaming columns by index
dataFrame.columns.values[0] = "Car Names"
dataFrame.columns.values[1] = "Registration Cost"
dataFrame.columns.values[2] = "Units_Sold"
print("\nDataFrame after renaming columns:")
print(dataFrame)
Original DataFrame:
Car Reg_Price Units
0 BMW 7000 90
1 Lexus 1500 120
2 Tesla 5000 100
3 Mustang 8000 150
4 Mercedes 9000 200
5 Jaguar 6000 130
DataFrame after renaming columns:
Car Names Registration Cost Units_Sold
0 BMW 7000 90
1 Lexus 1500 120
2 Tesla 5000 100
3 Mustang 8000 150
4 Mercedes 9000 200
5 Jaguar 6000 130
Renaming Specific Columns
You can also rename only specific columns by targeting their index positions ?
import pandas as pd
# Create DataFrame
df = pd.DataFrame({
"A": [1, 2, 3],
"B": [4, 5, 6],
"C": [7, 8, 9]
})
print("Before renaming:")
print(df.columns.tolist())
# Rename only first and last columns
df.columns.values[0] = "First_Column"
df.columns.values[2] = "Last_Column"
print("\nAfter renaming:")
print(df.columns.tolist())
print("\nDataFrame:")
print(df)
Before renaming: ['A', 'B', 'C'] After renaming: ['First_Column', 'B', 'Last_Column'] DataFrame: First_Column B Last_Column 0 1 4 7 1 2 5 8 2 3 6 9
How It Works
The columns.values property returns a NumPy array containing column names. By accessing specific indices like columns.values[0], you can directly modify individual column names. This method modifies the DataFrame in-place without creating a copy.
Key Points
- Index 0 refers to the first column, index 1 to the second column, and so on
- This method modifies the original DataFrame (in-place operation)
- Negative indices work too:
columns.values[-1]refers to the last column - More memory-efficient than
rename()as it doesn't create copies
Conclusion
Using columns.values[index] provides a direct way to rename DataFrame columns by position. This approach is efficient for renaming multiple columns when you know their positions, especially useful in data preprocessing pipelines.
