Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
iloc Basics | The Very First Steps
Pandas First Steps
course content

Course Content

Pandas First Steps

Pandas First Steps

1. The Very First Steps
2. Reading Files in Pandas
3. Analyzing the Data

bookiloc Basics

You can also access rows in a DataFrame by their index. There are multiple ways to do this:

  • .iloc - is used to access rows by their numerical index, starting from 0;
  • .loc - is used to access rows by their string label.

In this course, we will focus exclusively on using the .iloc attribute.

First, let's create a DataFrame to work with.

12345
import pandas dataset = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pandas.DataFrame(dataset) print(countries)
copy

The DataFrame has the following structure:

You can notice the first column, which serves as the row index. We'll use these indexes to access specific rows in the DataFrame. Now let's examine the syntax of this attribute:

Now it's time to apply this function to our DataFrame.

12345678
import pandas dataset = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pandas.DataFrame(dataset) # Accessing to the third and seventh rows print(countries.iloc[2]) print(countries.iloc[6])
copy

After running the above code, you'll get rows that correspond to the indexes indicated in the image below:

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 13
We're sorry to hear that something went wrong. What happened?
some-alt