DataFrame Head and Tail

pandas
Published

November 19, 2023

Understanding .head()

The .head() method allows you to display the top n rows of your DataFrame. By default, it shows the first 5 rows. You can adjust this number by passing an integer argument.

import pandas as pd

data = {'col1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        'col2': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']}
df = pd.DataFrame(data)

print(df.head())

print(df.head(3))

This code snippet first creates a sample DataFrame. Then, it demonstrates the default behavior of .head() and how to specify the number of rows to display.

Working with .tail()

Similar to .head(), the .tail() method displays the bottom n rows of your DataFrame. The default is also 5 rows, and you can customize it with an integer argument.

import pandas as pd

print(df.tail())

print(df.tail(2))

This code reuses the DataFrame and shows how to view the last rows using .tail(), both with the default and a specified number of rows.

Combining .head() and .tail() for Efficient Data Exploration

Using both .head() and .tail() together provides a rapid overview of your dataset, allowing you to quickly assess the beginning and end for potential anomalies or patterns. This is particularly useful when dealing with large datasets where examining the entire DataFrame would be impractical.

import pandas as pd

data = {'col1': range(100), 'col2': [chr(i+65) for i in range(100)]} # Generates 100 rows
df_large = pd.DataFrame(data)

print("First 5 rows:\n", df_large.head())
print("\nLast 5 rows:\n", df_large.tail())

This example shows how to use .head() and .tail() to efficiently inspect a larger DataFrame, avoiding the need to display all 100 rows.