Pandas: Rename a single column

There are several ways that you can rename multiple column names. But most of them requires specifying all of the column names, which can be time-consuming to create. What if you want to rename just one column?

There are a few ways to do that. One of them involves passing a dictionary to df.rename(). The keys of the dictionary will be replaced by the values.

df = pd.DataFrame({'Division': [1, 4, 7, 10], 'Year': [2012, 2014, 2013, 2014], 'SaleCount': [55, 40, 84, 31]} ) col_replace = { 'SaleCount': 'sale', } df.rename(columns=col_replace,inplace=True) df
Code language: Python (python)
DivisionYearsale
01201255
14201440
27201384
310201431

Leave a Comment