How to Rename Pandas Dataframe Column?

5 minutes read

To rename a column in a pandas DataFrame, you can use the rename() method and specify the old column name as the key and the new column name as the value in a dictionary. For example, if you have a DataFrame called df and you want to rename the column "old_name" to "new_name", you can use the following code:

1
df.rename(columns={"old_name": "new_name"}, inplace=True)


Make sure to set inplace=True so that the changes are applied to the original DataFrame. Alternatively, you can assign the result to a new DataFrame if you want to keep the original unchanged:

1
new_df = df.rename(columns={"old_name": "new_name"})


This will create a new DataFrame called new_df with the updated column name.

Where to deploy Python Code in November 2024?

1
DigitalOcean

Rating is 5 out of 5

DigitalOcean

2
AWS

Rating is 4.9 out of 5

AWS

3
Vultr

Rating is 4.8 out of 5

Vultr

4
Cloudways

Rating is 4.7 out of 5

Cloudways


How to rename a pandas DataFrame column?

To rename a column in a Pandas DataFrame, you can use the rename() method.


Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import pandas as pd

# Create a DataFrame
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = pd.DataFrame(data)

# Rename column 'A' to 'New_Column'
df = df.rename(columns={'A': 'New_Column'})

print(df)


This will rename the column 'A' to 'New_Column' in the DataFrame.


What is the most efficient way to rename columns in pandas DataFrame?

The most efficient way to rename columns in a pandas DataFrame is to use the rename method. You can specify new column names using a dictionary where the keys are the current column names and the values are the new column names.


For example, if you have a DataFrame df with columns "A" and "B" and you want to rename them to "Column1" and "Column2" respectively, you can use the following code:

1
df.rename(columns={'A': 'Column1', 'B': 'Column2'}, inplace=True)


Setting inplace=True will modify the original DataFrame instead of creating a new one. This method is efficient because it does not require iterating over each row or column individually.


How to assign new names to columns in pandas DataFrame?

You can assign new names to columns in a pandas DataFrame by accessing the columns attribute of the DataFrame and assigning a list of new column names to it. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import pandas as pd

# Create a DataFrame
data = {'A': [1, 2, 3],
        'B': [4, 5, 6],
        'C': [7, 8, 9]}
df = pd.DataFrame(data)

# Display the original column names
print("Original column names:")
print(df.columns)

# Assign new column names
new_columns = ['X', 'Y', 'Z']
df.columns = new_columns

# Display the DataFrame with the new column names
print("\nDataFrame with new column names:")
print(df)


Output:

1
2
3
4
5
6
7
8
Original column names:
Index(['A', 'B', 'C'], dtype='object')

DataFrame with new column names:
   X  Y  Z
0  1  4  7
1  2  5  8
2  3  6  9



How to change the name of a column in pandas DataFrame?

You can change the name of a column in a pandas DataFrame by using the rename() method. Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import pandas as pd

# Create a sample DataFrame
data = {'A': [1, 2, 3],
        'B': [4, 5, 6],
        'C': [7, 8, 9]}

df = pd.DataFrame(data)

# Display the original DataFrame
print("Original DataFrame:")
print(df)

# Rename the column 'A' to 'Column1'
df = df.rename(columns={'A': 'Column1'})

# Display the DataFrame with the renamed column
print("\nDataFrame with renamed column:")
print(df)


Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Original DataFrame:
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

DataFrame with renamed column:
   Column1  B  C
0        1  4  7
1        2  5  8
2        3  6  9


In this example, we renamed the column 'A' to 'Column1' using the rename() method. You can specify the new name of the column inside the columns parameter as a dictionary where the key is the old column name and the value is the new column name.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To rename a column in pandas when the column name contains a space, you can use the rename function and specify the old column name with the space enclosed in quotes. For example, if you have a DataFrame df with a column named "First Name", you can ren...
In Pandas, renaming columns in a DataFrame can be done using the rename() function. This function allows you to change the names of one or more columns in a DataFrame. Here's how to do it:First, import the required libraries: pandas. import pandas as pd Cr...
To bulk rename files in PowerShell, you can use the Rename-Item cmdlet.First, navigate to the directory containing the files you want to rename using the Set-Location cmdlet.You can use various parameters with the Rename-Item cmdlet to rename files based on sp...