how to change header of dataframe in python code example

Example 1: pandas rename column

df.rename(columns={"old_col1": "new_col1", "old_col2": "new_col2"}, inplace=True)

Example 2: df change column names

df.rename(columns={"A": "a", "B": "b", "C": "c"},
errors="raise", inplace=True)

Example 3: how to create new header of a dataframe in python

pythonCopy# python 3.x
import pandas as pd
import numpy as np

df = pd.DataFrame(data=np.random.randint(0, 10, (6,4)))

df.columns=["a", "b", "c", "d"]
print(df)

Example 4: how to change a header in pandas

# Can just use df.columns to rename

>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
>>> df.columns = ['a', 'b']
>>> df
   a   b
0  1  10
1  2  20