Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Access Index of Last Element in pandas DataFrame in Python
To access the index of the last element in a pandas DataFrame, we can use the Let's first create a DataFrame to work with throughout our examples ? The The DataFrames can have custom indices. Here's how to handle the last index in such cases ? Use index attribute or the tail()
Creating a Sample DataFrame
import pandas as pd
# Create sample DataFrame
df = pd.DataFrame({
'Subject': ['Maths', 'Science', 'Computer', 'English'],
'Marks': [70, 85, 90, 45],
'Grade': ['B', 'A', 'A', 'C']
})
print("Original DataFrame:")
print(df)
Original DataFrame:
Subject Marks Grade
0 Maths 70 B
1 Science 85 A
2 Computer 90 A
3 English 45 C
Using Index Attribute
index attribute provides access to row indices. To get the last row index, use negative indexing with -1 ?Syntax
df.index[-1]
Example
import pandas as pd
df = pd.DataFrame({
'Subject': ['Maths', 'Science', 'Computer', 'English'],
'Marks': [70, 85, 90, 45],
'Grade': ['B', 'A', 'A', 'C']
})
# Get the last row index
last_index = df.index[-1]
print("Last row index:", last_index)
# Alternative: Get all indices and select the last one
all_indices = df.index.tolist()
print("All indices:", all_indices)
print("Last index using max():", max(df.index))
Last row index: 3
All indices: [0, 1, 2, 3]
Last index using max(): 3
Using tail() Method
tail(n) method returns the last n rows. Combined with the index attribute, it provides another way to access the last row index ?Syntax
df.tail(1).index[0]
Example
import pandas as pd
df = pd.DataFrame({
'Subject': ['Maths', 'Science', 'Computer', 'English'],
'Marks': [70, 85, 90, 45],
'Grade': ['B', 'A', 'A', 'C']
})
# Get last row using tail()
last_row = df.tail(1)
print("Last row:")
print(last_row)
# Get the index of the last row
last_index = df.tail(1).index[0]
print("\nLast row index using tail():", last_index)
Last row:
Subject Marks Grade
3 English 45 C
Last row index using tail(): 3
Working with Custom Indices
import pandas as pd
# Create DataFrame with custom index
df_custom = pd.DataFrame({
'Subject': ['Maths', 'Science', 'Computer', 'English'],
'Marks': [70, 85, 90, 45],
'Grade': ['B', 'A', 'A', 'C']
}, index=['A', 'B', 'C', 'D'])
print("DataFrame with custom index:")
print(df_custom)
# Get last index with custom indices
print("\nLast index:", df_custom.index[-1])
print("Last index using tail():", df_custom.tail(1).index[0])
DataFrame with custom index:
Subject Marks Grade
A Maths 70 B
B Science 85 A
C Computer 90 A
D English 45 C
Last index: D
Last index using tail(): D
Comparison of Methods
Method
Syntax
Performance
Use Case
index[-1]Direct indexing
Fastest
Simple index access
tail(1).index[0]Method chaining
Slower
When working with subsets
max(df.index)Aggregation
Moderate
Non-sequential indices
Conclusion
df.index[-1] for the fastest way to get the last row index. The tail() method is useful when you need both the last row data and its index. Both methods work with custom indices as well.
