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
- Use iloc[] to select last column of pandas dataframe.
- Use [] to select last column of pandas dataframe.
- Use tail() to select last column of pandas dataframe.
- Get last column of pandas dataframe as list on python.
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.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.