Head or Tail

We wish to obtain the top n or bottom n rows of a data frame. It is good practice to look at the top and bottom set of rows of a new data frame especially when reading a spreadsheet export. Typically encoding issues and non data content (e.g. comments) can be seen by inspecting those top or bottom n rows.

We wish to get the top n rows of a data frame. In this example, we get the top 10 rows of a data frame.

df.head(n=10)

Here is how this works:

  • Pandas DataFrame objects have a head() method.
  • head() returns 5 rows by default. We can control the number of rows returned via the n argument e.g. n=10.
  • head() returns all columns.

Tail

We wish to get the bottom n rows of a data frame.

df.tail(n=10)

Here is how this works:

  • Pandas DataFrame objects have a tail() method.
  • tail() returns 5 rows by default. We can control the number of rows returned via the n argument e.g. n=10.

Selected Columns

We wish to get the top / bottom n rows of a data frame but return only a selected set of columns.

df.loc[:, ['col_1', 'col_3']].head()

Here is how this works:

  • The preferred method to select columns by name in Pandas is .loc[] (see Selecting)
  • The same pattern works for df.loc[:, ['col_1', 'col_3']].tail().
PYTHON
I/O