In [1]:
# Renaming a Single Column
import pandas as pd
df = pd.DataFrame({
''''A'''': [1, 2, 3],
''''B'''': [4, 5, 6]
})
df.rename(columns={''''A'''': ''''Alpha''''}, inplace=True)
print(df)
Alpha B 0 1 4 1 2 5 2 3 6
In [2]:
# Renaming Multiple Columns
import pandas as pd
df = pd.DataFrame({
''''A'''': [1, 2, 3],
''''B'''': [4, 5, 6]
})
df.rename(columns={''''A'''': ''''Alpha'''', ''''B'''': ''''Beta''''}, inplace=True)
print(df)
Alpha Beta 0 1 4 1 2 5 2 3 6
In [3]:
#Using columns Attribute
import pandas as pd
df = pd.DataFrame({
''''A'''': [1, 2, 3],
''''B'''': [4, 5, 6]
})
df.columns = [''''Alpha'''', ''''Beta'''']
print(df)
Alpha Beta 0 1 4 1 2 5 2 3 6
In [4]:
# Renaming Columns with a Function
import pandas as pd
df = pd.DataFrame({
''''A'''': [1, 2, 3],
''''B'''': [4, 5, 6]
})
df.rename(columns=lambda x: x.lower(), inplace=True)
print(df)
a b 0 1 4 1 2 5 2 3 6