pandas dataframe set column names code example
Example 1: rename column name pandas dataframe
df.rename(columns={"old_col1": "new_col1", "old_col2": "new_col2"})
Example 2: pandas rename column
df.rename(columns={"old_col1": "new_col1", "old_col2": "new_col2"}, inplace=True)
Example 3: create dataframe with column names pandas
In [4]: import pandas as pd
In [5]: df = pd.DataFrame(columns=['A','B','C','D','E','F','G'])
In [6]: df
Out[6]:
Empty DataFrame
Columns: [A, B, C, D, E, F, G]
Index: []
Example 4: df change column names
df.rename(columns={"A": "a", "B": "b", "C": "c"},
errors="raise", inplace=True)
Example 5: how to change column name in pandas
print(df.rename(columns={'A': 'a', 'C': 'c'}))
# a B c
# ONE 11 12 13
# TWO 21 22 23
# THREE 31 32 33
Example 6: rename dataframe index column pandas
df.index.names = ['new_name']