Skip to Content

How to Get the Last Column of a Pandas Dataframe?

In this tutorial, we will know how to get the last column of a Pandas Dataframe. Later, we will understand the same with the help of a few examples.

How to get the last column of a Dataframe using Pandas?

We can get the last column of a Dataframe in the following ways:

  • by using the column index
  • by using column name/label

1. Fetching the last column by index :

In case we are not aware of the last column name then we can always access the last column by its index, using the iloc property.

Syntax: dataFrameName.iloc[:, -1]

Note: Pandas dataframes support negative indexing which can be helpful when trying to access values from the end of an indexable object (like dataframes). The -1 column index represents the last column in the dataframe.

2. Fetching the last column by its name or label :

In order to access the last column by the column name or label, we can use the loc property which allows you to access rows and columns of a dataframe by their labels.

Syntax: dataFrameName.loc[:, “lastColumnName”]

Examples:

We will now look at a few examples for a better understanding.

But before that, we will create a pandas dataframe that we will be using throughout this tutorial using the following command:

import pandas as pd

# employee data
data = {
    "Name": ["Jim", "Dwight", "Angela", "Tobi"],
    "Age": [26, 28, 27, 32],
    "Department": ["Sales", "Sales", "Accounting", "HR"]
}

# create pandas dataframe
df = pd.DataFrame(data)
# displays dataframe
df

Output:

pandas dataframe with employee information

Example 1: Access the last column using the column index

get the last column of a pandas dataframe

Let’s access the last column of the dataframe created above using its column index (column position). You can use -1 as the index of the last column.

# get the last column of the dataframe
df.iloc[:, -1]

Output:

0         Sales
1         Sales
2    Accounting
3            HR
Name: Department, dtype: object

Example 2: Access the last column using the column name

Alternatively, you can access any column using its name. Now if you know the name of the last column, you can directly access it using the loc property.

# get the column using its name
df.loc[:, "Department"]

Output:

0         Sales
1         Sales
2    Accounting
3            HR
Name: Department, dtype: object

You can also directly use the column name using the square bracket notation of the dataframe to access it.

# get the column using its name
df["Department"]

Output:

0         Sales
1         Sales
2    Accounting
3            HR
Name: Department, dtype: object

We get the same result as above.

Summary:

In this tutorial, we looked at how to access the last column in a Pandas dataframe. The following are the key takeaways –

  • Use the iloc property to access the last column using its index, which is -1.
  • Use the loc property to access the last column using its name (or label). Alternatively, you can directly access the column by its name using the [] notation.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.