head() is almost always the first thing called after loading any new dataset — it returns a new DataFrame containing just the first n rows, letting you quickly verify column names, spot obvious formatting issues, and get a feel for the data's shape without printing the entire, possibly huge, DataFrame. Passing a negative n instead returns all rows except the last |n|, a lesser-known variant.
1Understanding df.head()
head() is almost always the first thing called after loading any new dataset — it returns a new DataFrame containing just the first n rows, letting you quickly verify column names, spot obvious formatting issues, and get a feel for the data's shape without printing the entire, possibly huge, DataFrame. Passing a negative n instead returns all rows except the last |n|, a lesser-known variant.
head() (and tail()) always return a genuine copy of the selected rows, not a view — modifying the result of df.head() never affects the original DataFrame.
import pandas as pd
df = pd.DataFrame({"id": range(1, 8), "value": [10, 20, 30, 40, 50, 60, 70]})
print(df.head())2Practical Example
Here is a real-world application of df.head() showing how it is used in production Pandas code.
import pandas as pd
df = pd.DataFrame({"id": range(1, 8)})
print(df.head(2))3Best Practices
Follow these guidelines when working with df.head():
1. Call df.head() immediately after loading any new dataset, as a quick sanity check on column names and formatting
2. Use a larger n temporarily when you need to inspect more rows than the default 5, rather than printing the whole DataFrame
3. Combine head() with .info()/.describe() for a fuller quick-inspection routine, since head() alone doesn't show dtypes or summary statistics
Tip: head() (and tail()) always return a genuine copy of the selected rows, not a view — modifying the result of df.head() never affects the original DataFrame.
import pandas as pd
df = pd.DataFrame({"id": range(1, 8), "value": [10, 20, 30, 40, 50, 60, 70]})
print(df.head())