How can I rename only the last column in a Pandas DataFrame?

How can I rename only the last column in a Pandas DataFrame?

To rename only the last column in a Pandas DataFrame, you can use the “rename” function and specify the column name and new name using the “columns” parameter. This will allow you to change the name of the last column without affecting any other columns in the DataFrame. Additionally, you can use the “inplace” parameter to make the changes directly to the original DataFrame. This method is useful for organizing and labeling data in a more meaningful way.

Pandas: Rename Only the Last Column in DataFrame


You can use the following basic syntax to rename only the last column in a pandas DataFrame:

df.columns = [*df.columns[:-1], 'new_name']

This particular example renames the last column new_name in a pandas DataFrame called df.

The following example shows how to use this syntax in practice.

Example: Rename Only the Last Column in Pandas

Suppose we have the following pandas DataFrame that contains information about various basketball players:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print(df)

  team  points  assists  rebounds
0    A      18        5        11
1    B      22        7         8
2    C      19        7        10
3    D      14        9         6
4    E      14       12         6
5    F      11        9         5
6    G      20        9         9
7    H      28        4        12

Currently the last column in the DataFrame is named rebounds.

We can use the following syntax to rename this column to rebs:

#rename last column to 'rebs'
df.columns = [*df.columns[:-1], 'rebs']

#view updated DataFrame
print(df)

  team  points  assists  rebs
0    A      18        5    11
1    B      22        7     8
2    C      19        7    10
3    D      14        9     6
4    E      14       12     6
5    F      11        9     5
6    G      20        9     9
7    H      28        4    12

Notice that the last column has been renamed to rebs and all other columns have remained unchanged.

We can also use the following syntax to view a list of all of the column names in the DataFrame:

#view column names
print(df.columns)

Index(['team', 'points', 'assists', 'rebs'], dtype='object')

We can see that the last column has indeed been renamed to rebs.

The benefit of using this syntax is that we don’t need to know ahead of time how many columns are in the DataFrame.

The following tutorials explain how to perform other common operations in pandas:

Cite this article

stats writer (2024). How can I rename only the last column in a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-rename-only-the-last-column-in-a-pandas-dataframe/

stats writer. "How can I rename only the last column in a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-rename-only-the-last-column-in-a-pandas-dataframe/.

stats writer. "How can I rename only the last column in a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-rename-only-the-last-column-in-a-pandas-dataframe/.

stats writer (2024) 'How can I rename only the last column in a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-rename-only-the-last-column-in-a-pandas-dataframe/.

[1] stats writer, "How can I rename only the last column in a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I rename only the last column in a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top