1df.rename(columns={'oldName1': 'newName1',
2 'oldName2': 'newName2'},
3 inplace=True, errors='raise')
4# Make sure you set inplace to True if you want the change
5# to be applied to the dataframe
1import pandas as pd
2data = pd.read_csv(file)
3data.rename(columns={'original':'new_name'}, inplace=True)
1# Basic syntax:
2# Assign column names to a Pandas dataframe:
3pandas_dataframe.columns = ['list', 'of', 'column', 'names']
4# Note, the list of column names must equal the number of columns in the
5# dataframe and order matters
6
7# Rename specific column names of a Pandas dataframe:
8pandas_dataframe.rename(columns={'column_name_to_change':'new_name'})
9# Note, with this approach, you can specify just the names you want to
10# change and the order doesn't matter
11
12# For rows, use "index". E.g.:
13pandas_dataframe.index = ['list', 'of', 'row', 'names']
14pandas_dataframe.rename(index={'row_name_to_change':'new_name'})
1df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
2
3# Option 1
4df.rename({"A": "a", "B": "c"}, axis=1)
5
6# Option 2
7df.rename(columns={"A": "a", "B": "c"})
8
9# Result
10 a c
110 1 4
121 2 5
132 3 6