Curriculum
Course: Pandas
Login
Text lesson

Pandas Analyzing Data

Viewing the Data

One of the most commonly used methods for quickly previewing a DataFrame is the head() method. It returns the headers along with a specified number of rows starting from the top.

Example

Obtain a quick overview by displaying the first 10 rows of the DataFrame:

import pandas as pd

df = pd.read_csv(‘data.csv’)

print(df.head(10))

For our examples, we will use a CSV file named ‘data.csv’.

Note: If the number of rows is not specified, the head() method will return the first 5 rows by default.

Example

Display the first 5 rows of the DataFrame:

import pandas as pd

df = pd.read_csv(‘data.csv’)

print(df.head())

The tail() method is used to view the last rows of the DataFrame. It returns the headers along with a specified number of rows, starting from the bottom.

Example

Display the last 5 rows of the DataFrame:

print(df.tail())