How to Rename Column by Index in pandas

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 name 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

# 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)

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


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.


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

2. Rename Column Name by Index

If you wanted 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.


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). Key is an existing column name and 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.


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.


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 renaming column name by Index using rename() method and df.columns.values[idx]. By using rename() method you can also change single and multiple columns, for multiples you need to create a map with key-value pair for all columns you wanted to change.

References

pandas rename column index

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

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