1# Basic syntax:
2your_dataframe.columns = ['new', 'column', 'names']
3
4# Google "python change row or column names in pandas dataframe" for
5# longer answer with examples
1# python 3.x
2import pandas as pd
3import numpy as np
4
5df = pd.DataFrame(data=np.random.randint(0, 10, (6,4)))
6
7df.columns=["a", "b", "c", "d"]
8print(df)
1#suppose team is a df that has unnamed columns (0,1...)
2team.columns =['Name', 'Code', 'Age', 'Weight']
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
1>gapminder.columns = ['country','year','population',
2 'continent','life_exp','gdp_per_cap']
3