Example 1: rename column pandas
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
>>> df.rename(columns={"A": "a", "B": "c"})
a c
0 1 4
1 2 5
2 3 6
Example 2: rename columns pandas
df.rename(columns={'oldName1': 'newName1',
'oldName2': 'newName2'},
inplace=True, errors='raise')
# Make sure you set inplace to True if you want the change
# to be applied to the dataframe
Example 3: python how to rename columns in pandas dataframe
# Basic syntax:
# Assign column names to a Pandas dataframe:
pandas_dataframe.columns = ['list', 'of', 'column', 'names']
# Note, the list of column names must equal the number of columns in the
# dataframe and order matters
# Rename specific column names of a Pandas dataframe:
pandas_dataframe.rename(columns={'column_name_to_change':'new_name'})
# Note, with this approach, you can specify just the names you want to
# change and the order doesn't matter
# For rows, use "index". E.g.:
pandas_dataframe.index = ['list', 'of', 'row', 'names']
pandas_dataframe.rename(index={'row_name_to_change':'new_name'})
Example 4: rename df column
import pandas as pd
data = pd.read_csv(file)
data.rename(columns={'original':'new_name'}, inplace=True)
Example 5: renaming column in dataframe pandas
df.rename({'a': 'X', 'b': 'Y'}, axis=1, inplace=True)
df
X Y c d e
0 x x x x x
1 x x x x x
2 x x x x x
Example 6: how to give name to column in pandas
>gapminder.rename(columns={'pop':'population',
'lifeExp':'life_exp',
'gdpPercap':'gdp_per_cap'},
inplace=True)
>print(gapminder.columns)
Index([u'country', u'year', u'population', u'continent', u'life_exp',
u'gdp_per_cap'],
dtype='object')
>gapminder.head(3)
country year population continent life_exp gdp_per_cap
0 Afghanistan 1952 8425333 Asia 28.801 779.445314
1 Afghanistan 1957 9240934 Asia 30.332 820.853030
2 Afghanistan 1962 10267083 Asia 31.997 853.100710