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
Get last n records of a Pandas DataFrame
When working with large datasets in Pandas, you often need to examine the most recent entries. The tail() method provides an efficient way to retrieve the last n records from a DataFrame.
Syntax
DataFrame.tail(n=5)
Parameters:
n Number of rows to return from the end (default is 5)
Creating a Sample DataFrame
Let's create a DataFrame to demonstrate the tail() method ?
import pandas as pd
data = {'Name': ['John', 'Mark', 'Alice', 'Julie', 'Lisa', 'David'],
'Age': [23, 34, 45, 19, 28, 31],
'Gender': ['M', 'M', 'F', 'F', 'F', 'M']}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
Original DataFrame:
Name Age Gender
0 John 23 M
1 Mark 34 M
2 Alice 45 F
3 Julie 19 F
4 Lisa 28 F
5 David 31 M
Getting Last n Records
Get Last 3 Records
import pandas as pd
data = {'Name': ['John', 'Mark', 'Alice', 'Julie', 'Lisa', 'David'],
'Age': [23, 34, 45, 19, 28, 31],
'Gender': ['M', 'M', 'F', 'F', 'F', 'M']}
df = pd.DataFrame(data)
last_3_records = df.tail(3)
print("Last 3 records:")
print(last_3_records)
Last 3 records:
Name Age Gender
3 Julie 19 F
4 Lisa 28 F
5 David 31 M
Reset Index (Optional)
You can reset the index to start from 0 using reset_index() ?
import pandas as pd
data = {'Name': ['John', 'Mark', 'Alice', 'Julie', 'Lisa', 'David'],
'Age': [23, 34, 45, 19, 28, 31],
'Gender': ['M', 'M', 'F', 'F', 'F', 'M']}
df = pd.DataFrame(data)
last_3_records = df.tail(3).reset_index(drop=True)
print("Last 3 records with reset index:")
print(last_3_records)
Last 3 records with reset index:
Name Age Gender
0 Julie 19 F
1 Lisa 28 F
2 David 31 M
Different Values of n
import pandas as pd
data = {'Name': ['John', 'Mark', 'Alice', 'Julie', 'Lisa', 'David'],
'Age': [23, 34, 45, 19, 28, 31],
'Gender': ['M', 'M', 'F', 'F', 'F', 'M']}
df = pd.DataFrame(data)
print("Last 1 record:")
print(df.tail(1))
print("\nLast 2 records:")
print(df.tail(2))
print("\nDefault (last 5 records):")
print(df.tail())
Last 1 record:
Name Age Gender
5 David 31 M
Last 2 records:
Name Age Gender
4 Lisa 28 F
5 David 31 M
Default (last 5 records):
Name Age Gender
1 Mark 34 M
2 Alice 45 F
3 Julie 19 F
4 Lisa 28 F
5 David 31 M
Common Use Cases
Data Validation Check recent data entries after insertion
Time Series Analysis Examine the latest observations
Model Training Use recent data for machine learning models
Data Monitoring Monitor the most recent records in streaming data
Conclusion
The tail() method provides a simple and efficient way to retrieve the last n records from a Pandas DataFrame. Use reset_index(drop=True) when you need the resulting DataFrame to have a continuous index starting from 0.
