Pandas: Select last N columns of dataframe

In this article, we will discuss different ways to select the last N columns of a dataframe in pandas.

Table of Contents

There are different ways to select the last N columns of a dataframe. Let’s discuss them one by one,

Use iloc[] to select last N columns 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 last N columns of the dataframe. For example,

N = 3
# Select last N columns of dataframe
last_n_column  = df.iloc[: , -N:]

We selected a portion of dataframe object, that included all rows, but only last N columns 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 N columns of the dataframe use negative index and and select from column index -N till end i.e (-N:) and select all rows using default values (:),

N = 3
# Select last 3 columns of dataframe
last_n_column  = df.iloc[: , -N:]

We provided the range to select the columns from -N position till the last column, to select the last N columns, therefore it returned a dataframe. Checkout complete example to select last N columns of dataframe using iloc,

import pandas as pd

# List of Tuples
empoyees = [('Jack',  34, 11, 51, 33, 34, 77, 88) ,
            ('Riti',  31, 12, 71, 56, 55, 99, 11) ,
            ('Aadi',  16, 13, 11, 44, 55, 33, 54) ,
            ('Mark',  41, 14, 12, 78, 89, 46, 56)]

# Create a DataFrame object
df = pd.DataFrame(  empoyees)

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

N = 3
# Select last 3 columns of dataframe
last_n_column  = df.iloc[: , -N:]

print("Last 3 Columns Of Dataframe : ")
print(last_n_column)

print('Type:')
print(type(last_n_column))

Output:

Contents of the Dataframe :
      0   1   2   3   4   5   6   7
0  Jack  34  11  51  33  34  77  88
1  Riti  31  12  71  56  55  99  11
2  Aadi  16  13  11  44  55  33  54
3  Mark  41  14  12  78  89  46  56
Last 3 Columns Of Dataframe :
    5   6   7
0  34  77  88
1  55  99  11
2  55  33  54
3  89  46  56
Type:
<class 'pandas.core.frame.DataFrame'>

We selected the last N columns of the dataframe.

Select last N columns of pandas dataframe using []

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

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

N = 3
# Select last 3 columns of dataframe
last_n_column = df[df.columns[-N:]]

print("Last 3 Columns Of Dataframe : ")
print(last_n_column)

print('Type:')
print(type(last_n_column))

Output:

Contents of the Dataframe :
      0   1   2   3   4   5   6   7
0  Jack  34  11  51  33  34  77  88
1  Riti  31  12  71  56  55  99  11
2  Aadi  16  13  11  44  55  33  54
3  Mark  41  14  12  78  89  46  56
Last 3 Columns Of Dataframe :
    5   6   7
0  34  77  88
1  55  99  11
2  55  33  54
3  89  46  56
Type:
<class 'pandas.core.frame.DataFrame'>

Use tail() to select the last N columns of pandas dataframe

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

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

N = 3
# Select last 3 columns
last_n_column = df.T.tail(3).T

print("Last 3 Columns Of Dataframe : ")
print(last_n_column)

print('Type:')
print(type(last_n_column))

Output:

Contents of the Dataframe :
      0   1   2   3   4   5   6   7
0  Jack  34  11  51  33  34  77  88
1  Riti  31  12  71  56  55  99  11
2  Aadi  16  13  11  44  55  33  54
3  Mark  41  14  12  78  89  46  56
Last 3 Columns Of Dataframe :
    5   6   7
0  34  77  88
1  55  99  11
2  55  33  54
3  89  46  56
Type:
<class 'pandas.core.frame.DataFrame'>

It returned the last N columns of dataframe as a dataframe object.

Summary

We learned different ways to get the last N columns of a dataframe in pandas.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top