🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.tail()

AI & DATA SCIENCE // df-tail

df.tail(n) returns the last n rows of a DataFrame (5 by default), the counterpart to head() for inspecting the end of a dataset.

Syntax

df.tail(n=5)

Deep Dive Course

tail() mirrors head() exactly, but from the end of the DataFrame instead of the beginning — useful for checking whether data loaded completely, since the last rows should look like valid, complete records, not truncated or garbage, or for inspecting the most recent entries in data that's ordered chronologically, like a time series or a log file loaded into a DataFrame. Like head(), a negative n returns all rows except the first |n|.

1Understanding df.tail()

tail() mirrors head() exactly, but from the end of the DataFrame instead of the beginning — useful for checking whether data loaded completely, since the last rows should look like valid, complete records, not truncated or garbage, or for inspecting the most recent entries in data that's ordered chronologically, like a time series or a log file loaded into a DataFrame. Like head(), a negative n returns all rows except the first |n|.

💡

Check df.tail() after loading a large file to sanity-check that the whole file was read correctly — if the last rows look truncated, garbled, or unexpectedly short, the load likely stopped partway through.

editor.html
import pandas as pd

df = pd.DataFrame({"id": range(1, 8), "value": [10, 20, 30, 40, 50, 60, 70]})
print(df.tail())
localhost:3000

2Practical Example

Here is a real-world application of df.tail() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"id": range(1, 8)})
print(df.tail(2))
localhost:3000

3Best Practices

Follow these guidelines when working with df.tail():

1. Check tail() after loading a large or untrusted file, to verify the data wasn't truncated partway through

2. Use tail() specifically for chronologically-ordered data, like time series or logs, to inspect the most recent entries

3. Combine head() and tail() together for a quick sense of both the start and end of a dataset without printing the whole thing

⚠️

Tip: Check df.tail() after loading a large file to sanity-check that the whole file was read correctly — if the last rows look truncated, garbled, or unexpectedly short, the load likely stopped partway through.

editor.html
import pandas as pd

df = pd.DataFrame({"id": range(1, 8), "value": [10, 20, 30, 40, 50, 60, 70]})
print(df.tail())
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"id": range(1, 8), "value": [10, 20, 30, 40, 50, 60, 70]})
print(df.tail())
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"id": range(1, 8)})
print(df.tail(2))

Best Practices

  • Check tail() after loading a large or untrusted file, to verify the data wasn't truncated partway through
  • Use tail() specifically for chronologically-ordered data, like time series or logs, to inspect the most recent entries
  • Combine head() and tail() together for a quick sense of both the start and end of a dataset without printing the whole thing

Interview Question

Why is checking df.tail() specifically useful after loading a very large file, compared to just checking df.head()?

Hint: Think about what kind of loading problem head() alone wouldn't reveal.

head() only shows the beginning of the file, which would look perfectly normal even if the load process failed or was cut off partway through and silently produced an incomplete DataFrame. Checking tail() specifically verifies that data all the way through to the actual end of the file was read successfully — if the load was truncated, the last rows would look obviously wrong, incomplete, or missing entirely, a problem head() alone has no way to catch.

Exercises

MediumPractice using df.tail() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"id": range(1, 8), "value": [10, 20, 30, 40, 50, 60, 70]})
print(df.tail())

Frequently Asked Questions

Why is checking df.tail() specifically useful after loading a very large file, compared to just checking df.head()?

head() only shows the beginning of the file, which would look perfectly normal even if the load process failed or was cut off partway through and silently produced an incomplete DataFrame. Checking tail() specifically verifies that data all the way through to the actual end of the file was read successfully — if the load was truncated, the last rows would look obviously wrong, incomplete, or missing entirely, a problem head() alone has no way to catch.

Related Functions

df-headdf-shapedf-info