xxxxxxxxxx
#how to rename columns with:
data = data.rename(columns={'Old_column' : 'New_column'})
xxxxxxxxxx
df.rename(columns={'oldName1': 'newName1',
'oldName2': 'newName2'},
inplace=True, errors='raise')
# Make sure you set inplace to True if you want the change
# to be applied to the dataframe
xxxxxxxxxx
df.rename(columns={"old_col1": "new_col1", "old_col2": "new_col2"}, inplace=True)
xxxxxxxxxx
df.rename({"current": "updated"}, axis=1, inplace=True)
print(df.dtypes)
xxxxxxxxxx
df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})
# Or rename the existing DataFrame (rather than creating a copy)
df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)
xxxxxxxxxx
# Define df
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
# Option 1
df = df.rename({"A": "a", "B": "c"}, axis=1)
# or
df.rename({"A": "a", "B": "c"}, axis=1, inplace=True)
# Option 2
df = df.rename(columns={"A": "a", "B": "c"})
# or
df.rename(columns={"A": "a", "B": "c"}, inplace=True)
# Result
>>> df
a c
0 1 4
1 2 5
2 3 6
xxxxxxxxxx
df.rename(columns = {'old_col1':'new_col1', 'old_col2':'new_col2'}, inplace = True)
xxxxxxxxxx
import pandas as pd
# Creating a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Renaming the 'A' column to 'X'
df.rename(columns={'A': 'X'}, inplace=True)
# Printing the updated dataframe
print(df)