In [1]:
import pandas as pd
In [2]:
df = pd.DataFrame({'A': [11, 21, 31],
'B': [12, 22, 32],
'C': [13, 23, 33]},
index=['ONE', 'TWO', 'THREE'])
In [3]:
print(df)
In [4]:
df_new = df.rename(columns={'A': 'a'}, index={'ONE': 'one'})
print(df_new)
In [5]:
print(df)
In [6]:
print(df.rename(columns={'A': 'a', 'C': 'c'}))
In [7]:
df_org = df.copy()
df_org.rename(columns={'A': 'a'}, index={'ONE': 'one'}, inplace=True)
print(df_org)
In [8]:
print(df.rename(columns=str.lower, index=str.title))
In [9]:
print(df.rename(columns=lambda s: s*3, index=lambda s: s + '!!'))
In [10]:
print(df.add_prefix('X_'))
In [11]:
print(df.add_suffix('_X'))
In [12]:
df.index = [1, 2, 3]
df.columns = ['a', 'b', 'c']
In [13]:
print(df)
In [14]:
# df.index = [1, 2, 3, 4]
# ValueError: Length mismatch: Expected axis has 3 elements, new values have 4 elements