Display First 10 Rows Of Dataframe

4 min read

How to Display the First 10 Rows of a DataFrame in Pandas

Introduction

Every time you start working with tabular data in Python, the pandas library is the de‑facto standard for creating and manipulating DataFrame objects. That's why this simple operation gives you a quick glimpse of column names, data types, missing values, and overall structure, helping you form hypotheses before diving into deeper analysis. Think about it: the most straightforward way to do this is to display the first ten rows of a DataFrame. Consider this: one of the first exploratory steps you’ll take is to see what the data actually looks like. In this article we’ll walk through the most common methods, explain when to use each one, and share best practices to ensure your data inspection is both efficient and reliable.

Why You Might Need the First 10 Rows

Before we jump into code, it’s useful to understand the practical motivations behind viewing just the first ten rows:

  • Quick sanity check – Verify that the file was read correctly and that no unexpected columns appear.
  • Column naming and data type inspection – Spot camelCase vs snake_case naming conventions or detect columns that should be numeric but are read as objects.
  • Identify missing or malformed entries – See if there are blank cells, inconsistent formatting, or outliers early on.
  • Guide further processing – Knowing the shape of the data helps you decide on cleaning steps, feature engineering, or model selection.

Because the first ten rows are usually representative of the overall dataset (especially for well‑structured CSVs, SQL exports, or API responses), they serve as an effective “preview” pane.

The Most Common Method: Using .head()

The pandas DataFrame class includes a built‑in method called .Which means head(). By default it returns the first five rows, but you can pass an integer argument to request any number of rows you want.

How .head() Works

import pandas as pd

# Load a sample DataFrame
df = pd.read_csv('sample_data.csv')

# Display the first 10 rows
first_ten = df.head(10)
print(first_ten)
  • Syntax: DataFrame.head(n=5)
  • Return type: A new DataFrame containing the first n rows.
  • Performance: Very fast because pandas internally slices the underlying data without copying the entire object.

Expected Output

   id   name  age  salary
0   1  Alice    25   50000
1   2    Bob    30   60000
2   3  Carol    22   45000
3   4   David    28   70000
4   5   Eve     35   80000
5   6   Frank   40   90000
6   7   Grace   31   65000
7   8   Heidi   27   55000
8   9   Ivan    33   75000
9  10   Judy    29   85000

The printed table mirrors the original DataFrame’s columns and index, making it ideal for a quick visual check.

Alternative Techniques

While .head() is the go‑to method for most scenarios, pandas offers several other ways to extract the first ten rows. Understanding these alternatives can help you write more flexible code, especially when you need to combine row selection with other operations That's the part that actually makes a difference. Nothing fancy..

Using .iloc[0:10]

.iloc is an integer‑location indexer that works on positional indices, regardless of the actual index labels Most people skip this — try not to..

first_ten_iloc = df.iloc[0:10]
print(first_ten_iloc)
  • Advantage: Works even if the DataFrame index is non‑sequential (e.g., after dropping rows).
  • Consideration: Returns a view when possible, which can be memory efficient but may raise a SettingWithCopyWarning if you later try to modify the slice.

Using .loc[0:9]

.loc is a label‑based indexer. If your index is a simple RangeIndex starting at 0, you can slice with labels Small thing, real impact..

first_ten_loc = df.loc[0:9]
print(first_ten_loc)
  • Note: This method is more intuitive when you have a custom index (e.g., dates or IDs) and you want the first ten labels rather than the first ten positions.

Using .sample(10, random_state=42) for a Random Preview

Sometimes you want a random sample of ten rows to get a broader sense of the data distribution, especially when the dataset is large and ordered (e., time‑series data). Think about it: g. The ` Nothing fancy..

random_preview = df.sample(n=10, random_state=42)
print(random_preview)
  • Use case: Spot‑checking data, checking for class imbalance, or validating that the dataset is not uniformly ordered.

Best Practices and Tips

When to Prefer .head() vs .iloc

  • Use .head() when you simply need a preview and want the most readable, self‑documenting code.
  • Choose .iloc when you need positional slicing and you’re comfortable handling potential view/copy warnings.
  • Pick .loc when your index holds meaningful labels and you want to slice by those labels.

Handling Large DataFrames

If you’re loading a massive CSV (hundreds of megabytes or more), calling .head(10) is still lightweight because pandas reads only the necessary metadata. Even so, if you notice that the display is truncated in your notebook, you can adjust the pandas option:

pd.set_option('display.max_rows', 10)   # Show only the first 10 rows by default
pd.set_option('display.max_columns', None)  # Show all columns

Combining with Other Methods

Often you’ll want to inspect not just the first ten rows but also some summary statistics or data types. head(10)with.You can chain .info() or `.

df.head(10).describe(include='all')

This pattern is handy when you need a concise report for a stakeholder who wants both a visual sample and high‑level statistics.

Common Pitfalls to Avoid

  1. Assuming the index starts at 0 – If you drop rows or load data with a custom index, `.loc[0:
Currently Live

Just Dropped

Connecting Reads

Picked Just for You

Thank you for reading about Display First 10 Rows Of Dataframe. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home