Book Image

Learning Pandas

By : Michael Heydt
Book Image

Learning Pandas

By: Michael Heydt

Overview of this book

Table of Contents (19 chapters)
Learning pandas
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 the zero-based positions specified in a list:

In [27]:
   # only take specific items
   s.take([0, 3, 9])

Out[27]:
   0     0
   3  ...