4 ways to Select last column of Pandas DataFrame

In this post, we are going to learn about 4 ways to Select last column of Pandas DataFrame.The Pandas library method tail(),iloc[], and subscript operator are used to select the last column of the Pandas dataframe.To run all the below programs make sure Pandas Libary is installed on the local system otherwise install it using this command pip install pandas

1. Select last column of Pandas DataFrame using tail()


To select the last column of dataframe by using tail() method, we have to first transpose(dfobj.T) the dataframe to convert rows to columns and vice-versa and select the first row using the tail(1). It selects a column of the original dataframe and again transposes the dataframe to get the last column of the dataframe.

import pandas as pd

data = {
    'Name': ['Jack', 'Jack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}

dfobj = pd.DataFrame(data)

df_Last_col = dfobj.T.tail(1).T

print('Last column of dataframe:',df_Last_col)

Output

Last Column Of Dataframe : 
  Subject
0    Math
1    Math
2    Math
3     Phy

2. Select last column of Pandas DataFrame using iloc[] as list


To select the last column of the dataframe as a list.We have used the iloc[] function and pass index or postion of column that is -1 mean the last column of dataframe.The tolist() method is used to convert the result into a list and return last column of dataframe as list.

  • Import Pandas library using “import pandas as pd”
  • Call the iloc[] method to select the last column of the dataframe.
  • Tolist() method to convert results to list.
import pandas as pd

data = {
    'Name': ['Jack', 'Jack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}

dfobj = pd.DataFrame(data)
 
df_last_col = dfobj.iloc[:, -1].tolist()
print(df_last_col)

Output

['Math', 'Math', 'Math', 'Phy']

3. Select last column of Pandas DataFrame using subscript notation


In this Python program example, The df.columns return label of dataframe columns,subscript operator is used to select the last column by passing negative -1 index.Let us understand with below example.

import pandas as pd

data = {
    'Name': ['Jack', 'Jack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}

dfobj = pd.DataFrame(data)
 
df_last_col = dfobj[dfobj.columns[-1]]
print(df_last_col)

Output

0    Math
1    Math
2    Math
3     Phy
Name: Subject, dtype: object

4. Select last column of Pandas DataFrame using iloc[]


To select the last column of the dataframe as a list.We have used the iloc[] function and pass index or position of the column that is -1 means the last column of the dataframe.

  • Import Pandas library using “import pandas as pd”
  • Call the iloc[] method to select the last column of the dataframe.
import pandas as pd

data = {
    'Name': ['Jack', 'Jack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}

dfobj = pd.DataFrame(data)
 
df_last_col = dfobj.iloc[:, -1]
print(df_last_col)

Output

0    Math
1    Math
2    Math
3     Phy
Name: Subject, dtype: object

Summary

In this post we have learned 4 ways to Select last column of Pandas DataFrame with examples by using The Pandas library method tail(),iloc[], and subscript operator.