O'Reilly logo

Learning pandas by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Peeking at data with heads, tails, and take

pandas provides the .head() and .tail() methods to examine just the first few, or last, records in a Series. By default, these return the first or last five rows, respectively, but you can use the n parameter or just pass an integer to specify the number of rows:

In [23]:
   # first five
   s.head()

Out[23]:
   0    0
   1    1
   2    1
   3    2
   4    3
   dtype: float64

In [24]:
   # first three
   s.head(n = 3) # s.head(3) is equivalent

Out[24]:
   0    0
   1    1
   2    1
   dtype: float64

In [25]:
   # last five
   s.tail()

Out[25]:
   5     4
   6     5
   7     6
   8     7
   9   NaN
   dtype: float64

In [26]:
   # last 3
   s.tail(n = 3) # equivalent to s.tail(3)

Out[26]:
   7     6
   8     7
   9   NaN
   dtype: float64

The .take() method will return the rows in a series that correspond to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required