Summary: in this tutorial, you’ll learn about how to rename columns of a DataFrame.
String operation on column names
Column names of a DataFrame are basically strings wrapped in Index
object. You can easily cast the df.columns
to a Python list for later usage if needed.
Pandas support string operations directly on DataFrame columns.
In the example below, we uses str.lower()
to convert column names to lowercase.
df = pd.DataFrame({'Month': [1, 4, 7, 10],
'Year': [2012, 2014, 2013, 2014],
'Sale': [55, 40, 84, 31]}
)
df.columns = df.columns.str.lower()
df
month | year | sale | |
---|---|---|---|
1 | 2012 | 55 | |
1 | 4 | 2014 | 40 |
2 | 7 | 2013 | 84 |
3 | 10 | 2014 | 31 |
Using the similar syntax, we can use .upper()
to make the DataFrame column names uppercase.
df.columns = df.columns.str.upper()
MONTH | YEAR | SALE | |
---|---|---|---|
1 | 2012 | 55 | |
1 | 4 | 2014 | 40 |
2 | 7 | 2013 | 84 |
3 | 10 | 2014 | 31 |
Python str.replace()
can also be used to clean out unnecessary part of column names.
df = pd.DataFrame({'meta=Division': [1, 4, 7, 10],
'meta=Year': [2012, 2014, 2013, 2014],
'meta=SaleCount': [55, 40, 84, 31]}
)
df.columns = df.columns.str.replace("meta=", "")
df
Division | Year | SaleCount | |
---|---|---|---|
1 | 2012 | 55 | |
1 | 4 | 2014 | 40 |
2 | 7 | 2013 | 84 |
3 | 10 | 2014 | 31 |
Rename columns
There are several ways you can rename column names in Pandas. You can simply replace the existing column names by passing a new list.
df = pd.DataFrame({'meta=Division': [1, 4, 7, 10],
'meta=Year': [2012, 2014, 2013, 2014],
'meta=SaleCount': [55, 40, 84, 31]}
)
df.columns = ['Code A', 'Code B', 'Code C']
df
Code A | Code B | Code C | |
---|---|---|---|
1 | 2012 | 55 | |
1 | 4 | 2014 | 40 |
2 | 7 | 2013 | 84 |
3 | 10 | 2014 | 31 |
Pandas also has a separate rename()
function that can also be used to change multiple column names using a dict
.
df = pd.DataFrame({'Division': [1, 4, 7, 10],
'Year': [2012, 2014, 2013, 2014],
'SaleCount': [55, 40, 84, 31]}
)
col_replace = {
'Year': 'yr',
'SaleCount': 'sales',
'Division': 'branch',
}
df.rename(columns=col_replace,inplace=True)
df
The original DataFrame column names will be replaced if it’s found in col_replace
dictionary.
