1df.rename(columns={"A": "a", "B": "b", "C": "c"},
2errors="raise", inplace=True)
3
1#df.rename() will only return a new df with the new headers
2#df = df.rename() will change the heders of the current dataframe
3df = df.rename(columns={"old_col1": "new_col1", "old_col2": "new_col2"})
1>gapminder.rename(columns={'pop':'population',
2 'lifeExp':'life_exp',
3 'gdpPercap':'gdp_per_cap'},
4 inplace=True)
5
6>print(gapminder.columns)
7
8Index([u'country', u'year', u'population', u'continent', u'life_exp',
9 u'gdp_per_cap'],
10 dtype='object')
11
12>gapminder.head(3)
13
14 country year population continent life_exp gdp_per_cap
150 Afghanistan 1952 8425333 Asia 28.801 779.445314
161 Afghanistan 1957 9240934 Asia 30.332 820.853030
172 Afghanistan 1962 10267083 Asia 31.997 853.100710
18