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
DivisionYearsale
1201255
14201440
27201384
310201431
Avatar photo
Author: Thijmen I’m currently a SysAdmin located in the Netherlands. Every day I try to keep around a hundred users happy with their network connections and overall, tech-related issues. I also spend my spare time fiddling with web-based applications.
See also  Pandas: How to rename columns

Leave a Comment