In this article, we will discuss different ways to get or select the last column of dataframe as a series or list object.

Table of Contents

There are different ways to select the last column of this dataframe. Let’s discuss them one by one,

Use iloc[] to select last column of pandas dataframe

In Pandas, the Dataframe provides an attribute iloc[], to select a portion of the dataframe using position based indexing. This selected portion can be few columns or rows . We can use this attribute to select only the last column of the dataframe. For example,

# Select last column of dataframe as a series
last_column = df.iloc[: , -1]

We selected a portion of dataframe that included all rows, but only the last column of the dataframe.

How did it work?

The syntax of dataframe.iloc[] is like,

df.iloc[row_start:row_end , col_start, col_end]

Arguments:

  • row_start: The row index/position from where it should start selection. Default is 0.
  • row_end: The row index/position from where it should end the selection i.e. select till row_end-1. Default is till the last row of the dataframe.
  • col_start: The column index/position from where it should start selection. Default is 0.
  • col_end: The column index/position from where it should end the selection i.e. select till end-1. Default is till the last column of the dataframe.

It returns a portion of the dataframe that includes rows from row_start to row_end-1 and columns from col_start to col_end-1.

To select the last column of dataframe use negative indexing i.e. select from last column (-1) till the end and select all rows using default values (:),

# Select last column of dataframe as a dataframe object
last_column = df.iloc[: , -1:]

We gave the range details i.e. ( -1: ) to select the last column, therefore it returned the dataframe. But if you want to select the last column of dataframe as a series object then just use -1 to select the last column i.e.

# Select last column of dataframe as a series
last_column = df.iloc[: , -1]

Checkout complete example to select last column of dataframe using iloc,

import pandas as pd

# List of Tuples
empoyees = [('Jack',    34, 'Sydney',   5),
            ('Riti',    31, 'Delhi' ,   7),
            ('Aadi',    16, 'London',   11),
            ('Mark',    41, 'Delhi' ,   12)]

# Create a DataFrame object
df = pd.DataFrame(  empoyees, 
                    columns=['Name', 'Age', 'City', 'Experience'])

print("Contents of the Dataframe : ")
print(df)

# Select last column of dataframe as a dataframe object
last_column = df.iloc[: , -1:]

print("Last Column Of Dataframe : ")
print(last_column)

print('Type: ', type(last_column))


# Select last column of dataframe as a series object
last_column = df.iloc[: , -1]

print("Last Column Of Dataframe : ")
print(last_column)

print('Type: ', type(last_column))

Output:

Contents of the Dataframe :
   Name  Age    City  Experience
0  Jack   34  Sydney           5
1  Riti   31   Delhi           7
2  Aadi   16  London          11
3  Mark   41   Delhi          12
Last Column Of Dataframe :
   Experience
0           5
1           7
2          11
3          12
Type:  <class 'pandas.core.frame.DataFrame'>
Last Column Of Dataframe :
0     5
1     7
2    11
3    12
Name: Experience, dtype: int64
Type:  <class 'pandas.core.series.Series'>

We selected the last column of dataframe as a series object.

Select last column of pandas dataframe using []

We can fetch the column names of dataframe as a sequence and then select the last column name. Then using that column name, we can select the last column of dataframe as a series object using subscript operator i.e. []. For example,

# Select Last Column 
last_column = df[df.columns[-1]]

print("Last Column Of Dataframe : ")
print(last_column)

print('Type: ', type(last_column))

Output:

Last Column Of Dataframe :
0     5
1     7
2    11
3    12
Name: Experience, dtype: int64
Type:  <class 'pandas.core.series.Series'>

Use tail() to select the last column of pandas dataframe

We can use the dataframe.T attribute to get a transposed view of the dataframe and then call the tail(1) function on that view to select the last row i.e. the last column of original dataframe. Then transpose back that series object to have the column contents as a dataframe object. For example,

# Select Last Column 
last_column = df.T.tail(1).T

print("Last Column Of Dataframe : ")
print(last_column)

print('Type: ', type(last_column)

Output:

Last Column Of Dataframe :
  Experience
0          5
1          7
2         11
3         12
Type:  <class 'pandas.core.frame.DataFrame'>

It returned the last column of dataframe as a dataframe object.

Pandas: Get last column of dataframe as list

Select the last column of dataframe as a series object using df.iloc[:, -1] and then call the tolist() function on the series object. It will return the last column of dataframe as a list object. For example,

# Select Last Column of dataframe as list
last_column = df.iloc[:, -1].tolist()

print("Last Column Of Dataframe : ")
print(last_column)

print('Type: ', type(last_column))

Output:

Last Column Of Dataframe :
[5, 7, 11, 12]
Type:  <class 'list'>

It returned the last column of dataframe as a list.

Summary

We learned different ways to get the last column of a dataframe as a series or list object in python.