Print First 10 Rows from a Pandas DataFrame in Python

When I was working on a large dataset containing US sales figures, I needed to quickly preview the data without loading the entire DataFrame. This is a common task when dealing with large datasets, and printing the first few rows can give you a quick overview of your data structure.

In this article, I will cover 5 simple methods you can use to display the first 10 rows of a Pandas DataFrame.

Let us start..

1. Use the head() Method

The simplest way to view the first few rows of a DataFrame is by using the head() method in Python. By default, this method returns the first 5 rows, but you can specify the number of rows you want to display.

Let’s start by creating a sample DataFrame with US sales data:

import pandas as pd
import numpy as np

# Create a sample DataFrame with US sales data
data = {
    'Date': pd.date_range(start='2023-01-01', periods=100),
    'State': np.random.choice(['California', 'Texas', 'New York', 'Florida', 'Illinois'], 100),
    'Product': np.random.choice(['Laptop', 'Smartphone', 'Tablet', 'Monitor', 'Keyboard'], 100),
    'Sales': np.random.randint(100, 5000, 100),
    'Units': np.random.randint(1, 50, 100)
}

df = pd.DataFrame(data)

Now, to display the first 10 rows of this DataFrame, simply use:

# Display the first 10 rows
print(df.head(10))

This will output the first 10 rows of your DataFrame, showing all columns. It’s clean, simple, and one of the most commonly used methods.

You can see the output in the screenshot below.

pandas get first row

2. Use DataFrame Indexing with iloc

Another way to get the first 10 rows is by using DataFrame indexing with the Python iloc method. This approach gives you more flexibility if you need to select specific rows and columns.

# Using iloc to get the first 10 rows
print(df.iloc[:10])

You can see the output in the screenshot below.

get first row of dataframe pandas

The :10 slice notation tells Pandas to return rows from index 0 up to (but not including) index 10.

If you also want to select specific columns, you can do:

# Get first 10 rows with specific columns
df.iloc[:10, [0, 1, 3]]  # Select first 10 rows and columns at positions 0, 1, and 3

This approach is particularly useful when you need to combine row and column selection in a single operation.

Read a CSV to the dictionary using Pandas in Python

3. Use DataFrame Slicing

DataFrame slicing is another intuitive way to get the first 10 rows:

# Using DataFrame slicing
first_10_rows = df[:10]
print(first_10_rows)

You can see the output in the screenshot below.

get first row of dataframe

This is essentially a shorthand for iloc indexing and will return the first 10 rows of your DataFrame. I find this approach particularly handy when I’m quickly exploring data in a Jupyter notebook.

Check out Python Dataframe Update Column Value

4. Use the nlargest() and nsmallest() Methods

If you want to see the first 10 rows sorted by a specific column, you can use the nsmallest() or nlargest() methods in Python. This is useful when you want to quickly identify the top or bottom values in your dataset.

# Get 10 rows with the highest sales
top_sales = df.nlargest(10, 'Sales')
print(top_sales)

# Get 10 rows with the lowest sales
bottom_sales = df.nsmallest(10, 'Sales')
print(bottom_sales)

This method not only gives you the first 10 rows but also sorts them based on the values in the specified column. I use this approach when I need to quickly identify top performers or issues in my datasets.

5. Use take() Method

Python method take() is less commonly used but provides another way to select specific rows by their indices:

# Using take() to get the first 10 rows
first_10 = df.take(range(10))
print(first_10)

This method is useful when you have a list of row indices that you want to select. In this case, we’re using range(10) to select the first 10 rows.

Combine Methods for More Complex Operations

Sometimes you might want to perform more complex operations when viewing your first 10 rows. For example, you might want to filter the data and then view the first 10 rows of the filtered data:

# Filter data for California sales and view first 10 rows
california_sales = df[df['State'] == 'California'].head(10)
print(california_sales)

# Get first 10 rows sorted by date and then filtered for high sales
recent_high_sales = df.sort_values('Date', ascending=False).head(10)[df['Sales'] > 3000]
print(recent_high_sales)

This flexibility allows you to quickly analyze specific segments of your data without processing the entire dataset.

Check out Convert Pandas Dataframe to Tensor Dataset

Performance Considerations for Large DataFrames

When working with very large DataFrames (millions of rows), efficiently viewing a sample of the data becomes even more important. All the methods I’ve shown are efficient for viewing data, but they differ slightly in performance:

  1. head() is optimized for displaying the first n rows
  2. iloc is generally fast for positional indexing
  3. Simple slicing (df[:10]) is a convenient shorthand for iloc
  4. nlargest() and nsmallest() have to sort the data, which can be more expensive

For truly massive datasets, you might want to consider using Dask or other libraries designed for big data if simply viewing the first 10 rows isn’t enough for your analysis.

I hope you found this article helpful for efficiently viewing your DataFrame data.

The methods that I explained in this article are: using the head() method, dataframe indexing with iloc, dataframe slicing, largest() and smallest() methods, and the take() method.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.