Learn to code for data analysis
Learn to code for data analysis

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

Free course

Learn to code for data analysis

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[]:

 CountryPopulation (1000s)TB deaths
0Afghanistan3055213000.00
1Albania317320.00
2Algeria392085100.00
3Andorra790.26
4Angola214726900.00

And, executing the following code will get and display the first seven rows in the dataframe df.

In []:

df.head(7)

Out[]:

 CountryPopulation (1000s)TB deaths
0Afghanistan3055213000.00
1Albania317320.00
2Algeria392085100.00
3Andorra790.26
4Angola214726900.00
5Antigua and Barbuda901.20
6Argentina41446570.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[]:

 CountryPopulation (1000s)TB deaths
189Venezuela (Bolivarian Republic of)30405480
190Viet Nam9168017000
191Yemen24407990
192Zambia145393600
193Zimbabwe141505700
LCDAB_1

Take your learning further371

Making the decision to study can be a big step, which is why you'll want a trusted University. The Open University has 50 years’ experience delivering flexible learning and 170,000 students are studying with us right now. Take a look at all Open University courses372.

If you are new to university level study, we offer two introductory routes to our qualifications. Find out Where to take your learning next?373 You could either choose to start with an Access courses374or an open box module, which allows you to count your previous learning towards an Open University qualification.

Not ready for University study then browse over 1000 free courses on OpenLearn375 and sign up to our newsletter376 to hear about new free courses as they are released.

Every year, thousands of students decide to study with The Open University. With over 120 qualifications, we’ve got the right course for you.

Request an Open University prospectus371