AppDividend
Latest Code Tutorials

Python Pandas: How To Rename DataFrame Column

0

Pandas.DataFrame.rename() is a function that changes any index or column names individually with dict, or It changes all index/column names with a function. The DataFrame.rename() method is quite useful when we need to rename some selected columns because we need to specify the information only for the columns which are to be renamed.

Pandas DataFrame rename column

Pandas DataFrame is rectangular grids that are used to store data. It is easy to visualize and work with a data when stored in the DataFrame. It consists of rows and columns. Each row is the measurement of some instance while the column is a vector which contains data for some particular attribute/variable.

Each dataframe column has comparable data throughout any specific column, but DataFrame rows can provide homogeneous or heterogeneous data throughout any particular row. Unlike two dimensional arrays, pandas dataframe axes are labeled.

See the following syntax of the rename() function.

Syntax

DataFrame.rename(self, mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False, level=None, errors='ignore')

Parameters

mapper: dict-like or function

Dict-like or functions transformations to apply to that axis’ values. Use either mapper and axis to specify the axis to target with the mapper, or Index and columns.

index: dict-like or function

Alternative to defining axis (mapper, axis=0 is equivalent to index=mapper).

columns: dict-like or function

Alternative to defining axis (mapper, axis=1 is equal to columns=mapper).

axis: int or str

Axis to target with the mapper. It can be either the axis name (‘index,’ ‘columns’) or number (0, 1). The default is ‘index’.

copy: bool, default True

Also, copy underlying data.

inplace: bool, default False

Whether to return a new DataFrame. If True, then the value of copy is ignored.

level: int or level name, default None

In the case of a MultiIndex, only rename labels at the specified level.

errors{‘ignore’, ‘raise’}, default ‘ignore’

If ‘raise’, raise a KeyError when a dict-like mapper, Index, or columns contain labels that are not present in the Index being transformed. If ‘ignore,’ existing keys will be renamed, and extra keys will be ignored.

Return Value

It returns DataFrame with the renamed axis labels.

It raises KeyError If any of the labels are not found in the selected axis and “errors=’raise’”.

Pandas DataFrame rename column Example

See the following code.

# app.py

import pandas as pd
import numpy as np

# reading the data
series = [('Stranger Things', 3, 'Millie'),
          ('Game of Thrones', 8, 'Emilia'),
          ('Westworld', 3, 'Evan Rachel'),
         ('La Casa De Papel', 4, 'Sergio')]

# Create a DataFrame object
dfObj = pd.DataFrame(series, columns=['Name', 'Seasons', 'Actor'])
print(dfObj)
df_new = dfObj.rename(columns={'Name': 'First Name'})
print(df_new)

In the above code, we have defined the DataFrame, and then we have used DataFrame.rename() function to change the column name from Name to First Name.

Output

python3 app.py
               Name  Seasons        Actor
0   Stranger Things        3       Millie
1   Game of Thrones        8       Emilia
2         Westworld        3  Evan Rachel
3  La Casa De Papel        4       Sergio
         First Name  Seasons        Actor
0   Stranger Things        3       Millie
1   Game of Thrones        8       Emilia
2         Westworld        3  Evan Rachel
3  La Casa De Papel        4       Sergio

Change multiple column names in Pandas (labels)

Multiple index/columns names changed at once by adding elements to a dictionary.

See the following code.

# app.py

import pandas as pd
import numpy as np

# reading the data
series = [('Stranger Things', 3, 'Millie'), ('Game of Thrones', 8, 'Emilia'),
          ('Westworld', 3, 'Evan Rachel'), ('La Casa De Papel', 4, 'Sergio')]

# Create a DataFrame object
print('Before changing the DataFrame columns')
dfObj = pd.DataFrame(series, columns=['Name', 'Seasons', 'Actor'])
print(dfObj)

# Change the column names using  rename() function
print('After changing the DataFrame columns')
df_new = dfObj.rename(columns={'Name': 'First Name', 'Actor': 'Hero'})
print(df_new)

Output

python3 app.py
Before changing the DataFrame columns
               Name  Seasons        Actor
0   Stranger Things        3       Millie
1   Game of Thrones        8       Emilia
2         Westworld        3  Evan Rachel
3  La Casa De Papel        4       Sergio
After changing the DataFrame columns
         First Name  Seasons         Hero
0   Stranger Things        3       Millie
1   Game of Thrones        8       Emilia
2         Westworld        3  Evan Rachel
3  La Casa De Papel        4       Sergio

In the above code, I have passed a dictionary that consists of the old name: new name. It will replace it with the new one.

Rename with functions or lambda expressions

Functions (callable objects) can also be specified in the parameter index and columns of the rename() method. Applying a function to convert the upper and lower case.

import pandas as pd
import numpy as np

# reading the data
series = [('Stranger Things', 3, 'Millie'), ('Game of Thrones', 8, 'Emilia'),
          ('Westworld', 3, 'Evan Rachel'), ('La Casa De Papel', 4, 'Sergio')]

# Create a DataFrame object
print('Before changing the DataFrame columns')
dfObj = pd.DataFrame(series, columns=['Name', 'Seasons', 'Actor'])
print(dfObj)

# Change the column names using  rename() function
print('After changing the DataFrame columns to lower case')
df_new = dfObj.rename(columns=str.lower)
print(df_new)

Output

python3 app.py
Before changing the DataFrame columns
               Name  Seasons        Actor
0   Stranger Things        3       Millie
1   Game of Thrones        8       Emilia
2         Westworld        3  Evan Rachel
3  La Casa De Papel        4       Sergio
After changing the DataFrame columns to lower case
               name  seasons        actor
0   Stranger Things        3       Millie
1   Game of Thrones        8       Emilia
2         Westworld        3  Evan Rachel
3  La Casa De Papel        4       Sergio

pandas.DataFrame.add_suffix()

Pandas.DataFrame.add_prefix() is a function that adds prefixes and

See the following code for the add_suffix() method.

# app.py

import pandas as pd
import numpy as np

# reading the data
series = [('Stranger Things', 3, 'Millie'), ('Game of Thrones', 8, 'Emilia'),
          ('Westworld', 3, 'Evan Rachel'), ('La Casa De Papel', 4, 'Sergio')]

# Create a DataFrame object
print('Before adding prefix to DataFrame columns')
dfObj = pd.DataFrame(series, columns=['Name', 'Seasons', 'Actor'])
print(dfObj)

# Change the column names using  rename() function
print('After adding prefix to DataFrame columns')
df_new = dfObj.add_prefix('KDL_')
print(df_new)

Output

python3 app.py
Before adding prefix to DataFrame columns
               Name  Seasons        Actor
0   Stranger Things        3       Millie
1   Game of Thrones        8       Emilia
2         Westworld        3  Evan Rachel
3  La Casa De Papel        4       Sergio
After adding prefix to DataFrame columns
           KDL_Name  KDL_Seasons    KDL_Actor
0   Stranger Things            3       Millie
1   Game of Thrones            8       Emilia
2         Westworld            3  Evan Rachel
3  La Casa De Papel            4       Sergio

pandas.DataFrame.add_suffix()

Pandas.DataFrame.add_suffix() method adds suffix to columns names.

See the following code.

import pandas as pd
import numpy as np

# reading the data
series = [('Stranger Things', 3, 'Millie'), ('Game of Thrones', 8, 'Emilia'),
          ('Westworld', 3, 'Evan Rachel'), ('La Casa De Papel', 4, 'Sergio')]

# Create a DataFrame object
print('Before adding prefix to DataFrame columns')
dfObj = pd.DataFrame(series, columns=['Name', 'Seasons', 'Actor'])
print(dfObj)

# Change the column names using  rename() function
print('After adding suffix to DataFrame columns')
df_new = dfObj.add_suffix('_KDL')
print(df_new)

Output

python3 app.py
Before adding suffix to DataFrame columns
               Name  Seasons        Actor
0   Stranger Things        3       Millie
1   Game of Thrones        8       Emilia
2         Westworld        3  Evan Rachel
3  La Casa De Papel        4       Sergio
After adding suffix to DataFrame columns
           Name_KDL  Seasons_KDL    Actor_KDL
0   Stranger Things            3       Millie
1   Game of Thrones            8       Emilia
2         Westworld            3  Evan Rachel
3  La Casa De Papel            4       Sergio

Pandas add_prefix() and add_suffix() only process columns. If you want to add prefixes or suffixes to Index, specify the lambda expression in the argument index with the rename() method as described above.

Also, add_prefix() and add_suffix() do not have inplace. If you want to update the original object, overwrite it like df = df.add_prefix().

How To Change and Row Names or Indexes in Pandas

Pandas rename() function is used to change row indexes or row names.

We just need to use the index argument and specify, we want to change the index not columns.

See the following code.

import pandas as pd
import numpy as np

# reading the data
series = [('Stranger Things', 3, 'Millie'), ('Game of Thrones', 8, 'Emilia'),
          ('Westworld', 3, 'Evan Rachel'), ('La Casa De Papel', 4, 'Sergio')]

# Create a DataFrame object
dfObj = pd.DataFrame(series, columns=['Name', 'Seasons', 'Actor'])
print(dfObj)

# After renaming the index values.
df_new = dfObj.rename(index={0:'zero',1:'one'})
print(df_new)

Output

python3 app.py
               Name  Seasons        Actor
0   Stranger Things        3       Millie
1   Game of Thrones        8       Emilia
2         Westworld        3  Evan Rachel
3  La Casa De Papel        4       Sergio
                  Name  Seasons        Actor
zero   Stranger Things        3       Millie
one    Game of Thrones        8       Emilia
2            Westworld        3  Evan Rachel
3     La Casa De Papel        4       Sergio

 

Conclusion

You can change the column name of a DataFrame in at least two ways.

One way is to use the df.columns from Pandas and assign new names directly.

Another way is to use the rename() function. Using pandas rename() to change column names is a much better way than before. One can change the names of specific columns easily. And not all the column names need to be changed.

To change column names using the rename() function in Pandas, one needs to specify the mapper, a dictionary with an old name as keys, and a new name as values.

See also

Pandas set_index()

Pandas iloc[]

Pandas value_counts()

Pandas sort_values()

Pandas pivot_table()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.