How to Rename Column by Index in pandas

  • Post author:
  • Post category:Pandas
  • Post last modified:October 5, 2023

You can rename pandas DataFrame column name by index (position) using rename() method or by assigning column name to df.columns.values[index]. In this article, I will explain renaming column names by Index on pandas DataFrame with examples.

1. Quick Examples of Rename Column by Index

Below are some quick examples of how to change column names by index on pandas DataFrame.


# Below are some quick examples

# Example 1: Assign column name by index
df.columns.values[1] = 'Courses_Fee'
print(df.columns)

# Example 2: Rename column name by index
df.rename(columns={df.columns[2]: 'Courses_Duration'},inplace=True)
print(df.columns)

# Example 3: Rename multiple columns
df.rename(columns={df.columns[1]: 'Fee', df.columns[2]: 'Duration'},inplace=True)
print(df.columns)

Let’s create a simple DataFrame to run these examples.


# Create DataFrame
import pandas as pd
technologies = [ ["Spark",20000, "30days"], 
                 ["Pandas",25000, "40days"], 
               ]
column_names=["Courses","Fee",'Duration']
df=pd.DataFrame(technologies, columns=column_names)
print(df.columns)

Yields below column names.


# Output:
Index(['Courses', 'Fee', 'Duration'], dtype='object')

2. Rename Column Name by Index

If you want to rename a single column by Index on pandas DataFrame then you can just assign a new value to the df.columns.values[idx], replace idx with the right index where you wanted to rename.


# Assign column name for Index
df.columns.values[1] = 'Courses_Fee'
print(df.columns)

Yields below output. df.columns get the column names as a list from pandas DataFrame.


# Output:
Index(['Courses', 'Courses_Fee', 'Duration'], dtype='object')

This changes the column name from Fee to Courses_Fee.

3. Using rename() to Change Column Name at Index

You can also use pandas DataFrame.rename() method to change column name at a specific index. This method takes the columns param with value as dict (key-value pair). The key is an existing column name and the value would be a new column name. Though this method doesn’t support by index, we can get the column name of the index by using df.columns[index].


# Rename single column by Index
df.rename(columns={df.columns[2]: 'Courses_Duration'},inplace=True)
print(df.columns)

Yields below output. Use inplace=True to replace on existing DataFrame object. not using this it returns a new DF without changing on an existing one.


# Output:
Index(['Courses', 'Courses_Fee', 'Courses_Duration'], dtype='object')

This changes the column name from Duration to Courses_Duration.

4. Rename Multiple Columns by Index

Now let’s see how to rename multiple column names by index/position in pandas DataFrame. For this, I will be using the same method explained above. To rename multiple, you have to pass multiple mappings in key-value pair to the columns param.


# Rename multiple columns by Index
df.rename(columns={df.columns[1]: 'Fee', df.columns[2]: 'Duration'},inplace=True)
print(df.columns)

Yields below output.


# Output:
Index(['Courses', 'Fee', 'Duration'], dtype='object')

The above example renames the multiple columns to the original values.

5. Complete Example


# Import pandas library
import pandas as pd
technologies = [ ["Spark",20000, "30days"], 
                 ["Pandas",25000, "40days"], 
               ]]

# Create DataFrame
column_names=["Courses","Fee",'Duration']
df=pd.DataFrame(technologies, columns=column_names)
print(df.columns)

# Assign column name by index
df.columns.values[1] = 'Courses_Fee'
print(df.columns)

# Rename column name by index
df.rename(columns={df.columns[2]: 'Courses_Duration'},inplace=True)
print(df.columns)

# Rename multiple columns

df.rename(columns={df.columns[1]: 'Fee', df.columns[2]: 'Duration'},inplace=True)
print(df.columns)

Conclusion

In this article you have learned to rename column names by Index using the rename() method and df.columns.values[idx]. By using the rename() method you can also change single and multiple columns, for multiples you need to create a map with a key-value pair for all columns you want to change.

References

Naveen

I am a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, I have honed my expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. My journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. I have started this SparkByExamples.com to share my experiences with the data as I come across. You can learn more about me at LinkedIn

Leave a Reply

You are currently viewing How to Rename Column by Index in pandas