How to select the first observation with Pandas

0 min

Knowing how to select the first observations of a DataFrame might come in handy when you are doing some data exploration.

Using the DataFrame.head() method will select the top 5 by default.

Selecting the first observations

import pandas as pd

# We read a sample dataset from the web.
df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')

print(df.head())
How to select the top 5 by default

Selecting the first 3 observations

import pandas as pd

# We read a sample dataset from the web.
df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')

print(df.head(3))
How to select the top 3

Here you are ! You now know how to select the first n observations of a DataFrame.