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: iloc(), head() and tail(). 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 theiloc 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 df, which is at index 0:
In []:
df.iloc[0]
Out[]:
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 df, which is at index 2:
In []:
df.iloc[2]
Out[]:
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 head() method.
You can tell head() is a method, rather than an attribute such as columns, 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.
In []:
df.head()
Out[]:
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.
In []:
df.head(7)
Out[]:
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 tail() method is similar to the head() 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 head() method.
In []:
df.tail()
Out[]:
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 |