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 = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})
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
# 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
# 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
xxxxxxxxxx
import pandas as pd
# Creating a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Displaying the DataFrame before changing column names
print(df)
# Renaming a single column
df = df.rename(columns={'A': 'NewA'})
# Renaming multiple columns
df = df.rename(columns={'A': 'NewA', 'B': 'NewB'})
# Displaying the DataFrame after changing column names
print(df)