🚀 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.head()

AI & DATA SCIENCE // df-head

df.head(n) returns the first n rows of a DataFrame (5 by default), the standard quick way to preview what a dataset looks like.

Syntax

df.head(n=5)

Deep Dive Course

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.

editor.html
import pandas as pd

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

2Practical Example

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

editor.html
import pandas as pd

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

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.

editor.html
import pandas as pd

df = pd.DataFrame({"id": range(1, 8), "value": [10, 20, 30, 40, 50, 60, 70]})
print(df.head())
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.head())
Example 02Advanced Example
import pandas as pd

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

Best Practices

  • Call df.head() immediately after loading any new dataset, as a quick sanity check on column names and formatting
  • Use a larger n temporarily when you need to inspect more rows than the default 5, rather than printing the whole DataFrame
  • Combine head() with .info()/.describe() for a fuller quick-inspection routine, since head() alone doesn't show dtypes or summary statistics

Interview Question

Does calling df.head() ever modify the original DataFrame, or trigger loading of the entire dataset if df was created lazily?

Hint: Think about what head() actually does under the hood — read-only inspection versus computing something new.

df.head() is a read-only operation — it returns a new DataFrame containing a copy of just the first n rows, and never modifies the original df in any way. For a DataFrame that's already fully loaded into memory, which is the normal case, head() doesn't need to touch or process the rest of the data at all; it's specifically because head() is such a common, cheap preview operation that some lazy-evaluation tools inspired by pandas' API go out of their way to optimize it and avoid computing unnecessary data.

Exercises

MediumPractice using df.head() 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.head())

Frequently Asked Questions

Does calling df.head() ever modify the original DataFrame, or trigger loading of the entire dataset if df was created lazily?

df.head() is a read-only operation — it returns a new DataFrame containing a copy of just the first n rows, and never modifies the original df in any way. For a DataFrame that's already fully loaded into memory, which is the normal case, head() doesn't need to touch or process the rest of the data at all; it's specifically because head() is such a common, cheap preview operation that some lazy-evaluation tools inspired by pandas' API go out of their way to optimize it and avoid computing unnecessary data.

Related Functions

df-taildf-infodf-describe