1.3 Getting and displaying dataframe rows
Dataframes can have hundreds or thousands of rows, so it is not practical to display a whole dataframe.
However, there are a number of dataframe attributes and methods that allow you to get and display either a single row or a number of rows at a time. Three of the most useful methods are:, and . Note that to distinguish methods and attributes, we write () after a method’s name.

The iloc attribute
A dataframe has a default integer index for its rows, which starts at 0 (zero). You can get and display any single row in a dataframe by using the attribute with the index of the row you want to access as its argument. For example, the following code will get and display the first row of data in the dataframe , which is at index 0:
df.iloc[0]
Country Afghanistan
Population (1000s) 30552
TB deaths 13000
Name: 0, dtype: object
Similarly, the following code will get and display the third row of data in the dataframe , which is at index 2:
df.iloc[2]
Country Algeria
Population (1000s) 39208
TB deaths 5100.0
Name: 0, dtype: object
The head() method
The first few rows of a dataframe can be printed out with the method.
You can tell is a method, rather than an attribute such as , because of the parentheses (round brackets) after the property name.
If you don’t give any argument, i.e. don’t put any number within those parentheses, the default behaviour is to return the first five rows of the dataframe. If you give an argument, it will print that number of rows (starting from the row indexed by 0).
For example, executing the following code will get and display the first five rows in the dataframe .
df.head()
| Country | Population (1000s) | TB deaths | |
|---|---|---|---|
| 0 | Afghanistan | 30552 | 13000.00 |
| 1 | Albania | 3173 | 20.00 |
| 2 | Algeria | 39208 | 5100.00 |
| 3 | Andorra | 79 | 0.26 |
| 4 | Angola | 21472 | 6900.00 |
And, executing the following code will get and display the first seven rows in the dataframe
df.head(7)
| Country | Population (1000s) | TB deaths | |
|---|---|---|---|
| 0 | Afghanistan | 30552 | 13000.00 |
| 1 | Albania | 3173 | 20.00 |
| 2 | Algeria | 39208 | 5100.00 |
| 3 | Andorra | 79 | 0.26 |
| 4 | Angola | 21472 | 6900.00 |
| 5 | Antigua and Barbuda | 90 | 1.20 |
| 6 | Argentina | 41446 | 570.00 |
The tail() method
The method is similar to the method.
If no argument is given, the last five rows of the dataframe are returned, otherwise the number of rows returned is dependent on the argument, just like for the method.
df.tail()
| Country | Population (1000s) | TB deaths | |
|---|---|---|---|
| 189 | Venezuela (Bolivarian Republic of) | 30405 | 480 |
| 190 | Viet Nam | 91680 | 17000 |
| 191 | Yemen | 24407 | 990 |
| 192 | Zambia | 14539 | 3600 |
| 193 | Zimbabwe | 14150 | 5700 |
OpenLearn - Introduction and guidance
Except for third party materials and otherwise, this content is made available under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 Licence, full copyright detail can be found in the acknowledgements section. Please see full copyright statement for details.
