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.
import pandas as pd
df = pd.DataFrame({"id": range(1, 8), "value": [10, 20, 30, 40, 50, 60, 70]})
print(df.tail())2Practical Example
Here is a real-world application of df.tail() showing how it is used in production Pandas code.
import pandas as pd
df = pd.DataFrame({"id": range(1, 8)})
print(df.tail(2))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.
import pandas as pd
df = pd.DataFrame({"id": range(1, 8), "value": [10, 20, 30, 40, 50, 60, 70]})
print(df.tail())