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
Python Pandas – How to use Pandas DataFrame tail( ) function
The Pandas DataFrame tail() function returns the last n rows of a DataFrame. This is particularly useful when combined with filtering operations to examine the bottom portion of your filtered data.
Syntax
DataFrame.tail(n=5)
Parameters:
- n (int, optional): Number of rows to select. Default is 5.
Creating Sample Data
Let's create a sample dataset to demonstrate the tail() function ?
import pandas as pd
# Create sample products data
data = {
'id': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'product': ['Laptop', 'Phone', 'Tablet', 'Watch', 'Camera', 'Speaker', 'Headset', 'Monitor', 'Keyboard', 'Mouse'],
'price': [45000, 25000, 35000, 15000, 55000, 8000, 12000, 42000, 3000, 2500],
'category': ['Electronics', 'Electronics', 'Electronics', 'Accessories', 'Electronics', 'Audio', 'Audio', 'Electronics', 'Accessories', 'Accessories']
}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
Original DataFrame: id product price category 0 1 Laptop 45000 Electronics 1 2 Phone 25000 Electronics 2 3 Tablet 35000 Electronics 3 4 Watch 15000 Accessories 4 5 Camera 55000 Electronics 5 6 Speaker 8000 Audio 6 7 Headset 12000 Audio 7 8 Monitor 42000 Electronics 8 9 Keyboard 3000 Accessories 9 10 Mouse 2500 Accessories
Using tail() with Filtering
Now let's filter products with price between 30000 to 70000 and get the last 3 rows ?
import pandas as pd
# Create sample data
data = {
'id': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'product': ['Laptop', 'Phone', 'Tablet', 'Watch', 'Camera', 'Speaker', 'Headset', 'Monitor', 'Keyboard', 'Mouse'],
'price': [45000, 25000, 35000, 15000, 55000, 8000, 12000, 42000, 3000, 2500]
}
df = pd.DataFrame(data)
# Filter products with price between 30000 to 70000
filtered_df = df[(df['price'] >= 30000) & (df['price'] <= 70000)]
print("Filtered products (price 30000-70000):")
print(filtered_df)
# Get last 3 rows with specific columns
result = filtered_df[['id', 'product']].tail(3)
print("\nLast 3 rows (id and product columns):")
print(result)
Filtered products (price 30000-70000): id product price 0 1 Laptop 45000 2 3 Tablet 35000 4 5 Camera 55000 7 8 Monitor 42000 Last 3 rows (id and product columns): id product 2 3 Tablet 4 5 Camera 7 8 Monitor
Method 1: Using between() Function
The between() method provides a clean way to filter numeric ranges ?
import pandas as pd
# Create sample data
data = {
'id': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'product': ['Laptop', 'Phone', 'Tablet', 'Watch', 'Camera', 'Speaker', 'Headset', 'Monitor', 'Keyboard', 'Mouse'],
'price': [45000, 25000, 35000, 15000, 55000, 8000, 12000, 42000, 3000, 2500]
}
df = pd.DataFrame(data)
# Using between() for price filtering
filtered_df = df[df['price'].between(30000, 70000)]
result = filtered_df[['id', 'product']].tail(3)
print(result)
id product 2 3 Tablet 4 5 Camera 7 8 Monitor
Method 2: Using Conditional Operators
Alternative approach using comparison operators for more explicit filtering ?
import pandas as pd
# Create sample data
data = {
'id': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'product': ['Laptop', 'Phone', 'Tablet', 'Watch', 'Camera', 'Speaker', 'Headset', 'Monitor', 'Keyboard', 'Mouse'],
'price': [45000, 25000, 35000, 15000, 55000, 8000, 12000, 42000, 3000, 2500]
}
df = pd.DataFrame(data)
# Using conditional operators
filtered_df = df[(df['price'] >= 30000) & (df['price'] <= 70000)]
result = filtered_df[['id', 'product']].tail(3)
print(result)
id product 2 3 Tablet 4 5 Camera 7 8 Monitor
Common Use Cases
The tail() function is commonly used for ?
- Examining the last few records after sorting
- Quality checking filtered datasets
- Getting recent entries in time-series data
- Sampling bottom rows for analysis
Comparison
| Method | Syntax | Best For |
|---|---|---|
between() |
df[col.between(min, max)] |
Clean range filtering |
| Conditional operators | df[(col >= min) & (col <= max)] |
Complex conditions |
iloc slicing |
df.iloc[-3:, 0:2] |
Position-based selection |
Conclusion
The tail() function is essential for examining the bottom rows of filtered DataFrames. Use between() for clean range filtering, or conditional operators for more complex conditions. Both methods work effectively with tail() to get the last n rows of your filtered data.
---