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:
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.We wish to get the bottom n
rows of a data frame.
df.tail(n=10)
Here is how this works:
tail()
method.tail()
returns 5 rows by default. We can control the number of rows returned via the n
argument e.g. n=10
.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:
.loc[]
(see Selecting)df.loc[:, ['col_1', 'col_3']].tail()
.