How to rename columns in pandas?

Published on Aug. 22, 2023, 12:18 p.m.

To rename columns in pandas

To rename columns in pandas, you can use the rename() method on a pandas DataFrame. rename() accepts a dictionary, where the keys are the old column names and the values are the new column names. Alternatively, you can use the columns parameter to pass a list of new column names in the desired order. Here are a few examples:

  1. Rename columns using a dictionary:
import pandas as pd

# create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'b', 'c']})

# rename columns
df = df.rename(columns={'A': 'new_A', 'B': 'new_B'})

print(df)

This will output the DataFrame with the column names changed:

   new_A new_B
0      1     a
1      2     b
2      3     c
  1. Rename columns using a list:
# rename columns using a list
df.columns = ['new_A', 'new_B']

print(df)

This will output the DataFrame with the column names changed:

   new_A new_B
0      1     a
1      2     b
2      3     c
  1. Using the set_axis() method:
df.set_axis(['new_name1', 'new_name2', 'new_name3', ...], axis=1, inplace=True)

Note that in each of these methods, the inplace=True parameter is used to modify the original DataFrame. If inplace is not used, a new DataFrame with the modified columns will be returned. Additionally, if you want to rename multiple columns at once, you can pass in a dictionary to the rename() method with the old and new column names as key-value pairs.

Note that the rename() method returns a new DataFrame with the column names changed, it does not modify the original DataFrame in place. If you want to modify the original DataFrame, you need to assign the result of rename() back to the original DataFrame or you can use the inplace parameter of rename() to change the DataFrame in place:

# modify the original DataFrame
df.rename(columns={'A': 'new_A', 'B': 'new_B'}, inplace=True)

print(df)

This will output the same DataFrame with the column names changed in place.

How do I replace spaces with underscores in column names using Pandas?

To replace spaces with underscores in column names using Pandas, you can use the str.replace() method on the columns attribute of your DataFrame. Here’s an example:

import pandas as pd

# Load sample data
df = pd.read_csv('mydata.csv')

# Replace spaces with underscores in column names
df.columns = df.columns.str.replace(' ', '_')

# Write the modified data back to a file
df.to_csv('mydata_modified.csv')

In the code above, the str.replace() method is used to replace all spaces in column names with underscores. The resulting modified DataFrame is then written to a new CSV file using the to_csv() method.

This will help you avoid any issues related to spaces in column names and make your column names compatible with various data storage and analysis systems.

How do I display the new column names after renaming them in Pandas?

To display the new column names after renaming them in Pandas, you can simply use the columns attribute of your DataFrame. Here’s an example:

import pandas as pd

# Load sample data
df = pd.read_csv('mydata.csv')

# Replace spaces with underscores in column names
df.columns = df.columns.str.replace(' ', '_')

# Display the new column names
print(df.columns)

In the code above, after renaming the columns in the DataFrame, we access the columns attribute of the DataFrame and print its contents to display the new column names.

This will help you verify that the column names have been successfully renamed and make sure that they are compatible with your other data analysis tools.

Tags: