xxxxxxxxxx
df.rename({'current':'updated'},axis = 1, inplace = True)
xxxxxxxxxx
#df.rename() will only return a new df with the new headers
#df = df.rename() will change the heders of the current dataframe
df = df.rename(columns={"old_col1": "new_col1", "old_col2": "new_col2"})
xxxxxxxxxx
df.rename(columns={"A": "a", "B": "b", "C": "c"},
errors="raise", inplace=True)
xxxxxxxxxx
df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})
xxxxxxxxxx
import pandas as pd
# Assuming the dataframe is already created and stored in a variable called 'df'
# Print the current column names
print(df.columns)
# Rename a specific column
df.rename(columns={'current_column_name': 'new_column_name'}, inplace=True)
# Print the updated column names
print(df.columns)
xxxxxxxxxx
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)
# Rename column 'A' to 'New Column'
df.rename(columns={'A': 'New Column'}, inplace=True)
# Print the modified dataframe
print(df)
xxxxxxxxxx
import pandas as pd
# Example DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Rename column 'A' to 'NewColumn'
df = df.rename(columns={'A': 'NewColumn'})
# Print the updated DataFrame
print(df)
xxxxxxxxxx
print(df.rename(columns={'A': 'a', 'C': 'c'}))
# a B c
# ONE 11 12 13
# TWO 21 22 23
# THREE 31 32 33