access index of last element in pandas

How to Access Index of Last Element in Pandas Dataframe

Sometimes you may need to access index of last element in pandas dataframe. In this article, we will learn how to do this in python.


How to Access Index of Last Element in Pandas Dataframe

We will use Dataframe.iloc to get data by specifying its index. The element indexes start from 0 for first row, 1 for second row and so on. On the other hand, python pandas also support negative index to help you quickly access the last elements. In this case, the last element will have index -1, the penultimate element will have index -2 and so on. Here is the syntax to use iloc command.

pandas.DataFrame.iloc[]

Here is a simple example to create a pandas dataframe and access the last element using iloc function.

# import pandas
import pandas as pd

# create dataframe
df = pd.DataFrame({'Name': ['Mukul', 'Rohan', 'Rahul',
							'Krish', 'Rohit'],
				'Course': ['BCA', 'MBA', 'MBA', 'BCA',
							'BBA'],
				'Address': ['Saharanpur', 'Mohali',
							'Saharanpur', 'Mohali',
							'Noida']})

# Display last index value of dataframe
# iloc[-1] is return the last element of
# all columns in DataFrame.
print("value of last index column")
print(df.iloc[-1])

You will see the following output when you run the above code.

value of last index column
Name Rohit
Course BBA
Address Noida
Name:4, dtype:object

As you can see above, the iloc command prints the last element of each column in the dataframe. If you want to print only specific column in dataframe, then you can specify that column in dataframe object, that is, use df[‘Address’].iloc(-1) instead of using df.iloc(-1). Here is an example.

# import pandas
import pandas as pd

# create dataframe
df = pd.DataFrame({'Name': ['Mukul', 'Rohan', 'Rahul',
							'Krish', 'Rohit'],
				'Course': ['BCA', 'MBA', 'MBA',
							'BCA', 'BBA'],
				'Address': ['Saharanpur', 'Mohali',
							'Saharanpur', 'Mohali',
							'Noida']})

# Display original dataframe
print("Original dataframe")
print(df)

# Display last index value of Address dataframe
print("last index value of Address Column: ", df['Address'].iloc[-1])

When you run the above code, you will see the following output.

last value of Address Column: Noida

You can also use dataframe.iat() function to access any element in your dataframe. Here is the syntax of this function.

Dataframe.iat[row, column]

Here is an example to return value of name column of last element.

# import pandas
import pandas as pd

# create dataframe
df = pd.DataFrame({'Name': ['sanjay', 'suresh',
							'Rahul', 'Krish',
							'vihan'],
				'Address': ['Haridwar', 'Mohali',
							'mohali', 'Mohali',
							'saharanpur']})

# Display original dataframe
print(" Original dataframe ")
print(df)

# Display last index value of 0 index column
print("last element value of 0 index column is ", df.iat[-1, 0])

When you run the above code, you will get the following output.

last element value of 0 index column is vihan

In this case, iat() function will only return one value at a time, unlike iloc that is capable of returning one or more values, as per your user requirement.

If you want to get only the index of last element, and not the value, you can use index() function for this purpose. Here is an example to get index of last element in all columns.

# import pandas
import pandas as pd

# create dataframe
df = pd.DataFrame({'Name': ['Mukul', 'Rohan',
							'Rahul', 'Krish',
							'Rohit'],
				'Address': ['Saharanpur', 'Mohali',
							'Saharanpur', 'Mohali',
							'Noida']})

# Display original dataframe
print(" Original dataframe ")
print(df)

# Display last index value of dataframe
# iloc[-1] is return the last element of
# all columns in DataFrame.
print(" last index is ", df.index[-1])

When you run the above code, you will get the following output.

last index is 4

In this article, we have learnt how to access index of last element in pandas dataframe.

Also read:

MySQL INSERT or UPDATE if Exists
How to Regular Expression Replace in MySQL
How to Get Digits from String in MySQL
How to Use SCP with PEM File (SSH Key)
How to Ignore Certificate Errors in cURL

Leave a Reply

Your email address will not be published.