Skip to content
Justice Tech School

Head, Tail and Sample

head shows the first rows, tail the last rows and sample draws random rows. Use random_state for repeatable samples.

Example
import pandas as pd
import numpy as np
df = pd.DataFrame({'name': ['Ada', 'Bob', 'Cy', 'Di'], 'age': [30, 25, 35, 28], 'city': ['Lagos', 'Accra', 'Lagos', 'Nairobi'], 'score': [88.5, 92.0, 79.5, 85.0]})
print(df.head(2))
print(df.tail(1))
print(df.sample(2, random_state=1).shape)

Result

  name  age   city  score
0  Ada   30  Lagos   88.5
1  Bob   25  Accra   92.0
  name  age     city  score
3   Di   28  Nairobi   85.0
(2, 4)

Exercise

Show the first 3 rows.

df.head()

Quiz Test

What is the default for head?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.