Obtaining last value of dataframe column without index

Given a pandas dataframe, we have to obtain the last value of dataframe column without index. By Pranit Sharma Last updated : October 03, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

Suppose we are given a DataFrame that contains a column of some random integer values and we need to retrieve the last value of the DataFrame without using the index.

Obtaining the last value of pandas dataframe column without index

For this purpose, we will use the pandas.DataFrame.iloc property. i in pandas.DataFrame.iloc property stands for index. This is also a data selection method but here, we need to pass the proper index as a parameter to select the required row or column. Indexes are nothing but integer value ranging from 0 to n-1 which represents the number of rows or columns. We can perform various operations using the pandas.DataFrame.iloc property. Inside the pandas.DataFrame.iloc property, the index value of the row comes first followed by the number of columns.

Let us understand with the help of an example,

Python program to obtain the last value of dataframe column without index

# Importing pandas package
import pandas as pd

# Creating a dictionary
d = {
    'a':[1,2,3,4,5,6,7,8,9],
    'b':[1,2,3,4,5,6,7,8,9],
    'c':[1,2,3,4,5,6,7,8,9],
    'd':[1,2,3,4,5,6,7,8,9],
    'e':[1,2,3,4,5,6,7,8,9]
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Accessing the last element 
# using iloc property
res = df['c'].iloc[-1]

# Display result
print("Result:\n",res)

Output

The output of the above program is:

Example: Obtaining last value of dataframe column without index

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.