Pandas iloc

In Python Pandas iloc stands for integer location. In this lesson we are going over 12 different examples of how we can utilize this to grab data within our dataframes.

If you want to watch a video tutorial of this lesson it is linked below.Â

Import in Pandas

  import pandas as pd

To start we’re going to create a simple dataframe in python. The data below is the top 10 Marathon times as of early 2025. It includes the rank, athlete, country, time, pace per mile, race, and year of time.

  data = { "Rank": range(1, 11), "Athlete": [ "Kelvin Kiptum", "Eliud Kipchoge", "Kenenisa Bekele", "Sisay Lemma", "Sabastian Sawe", "Benson Kipruto", "Deresa Geleta", "John Korir", "Birhanu Legese", "Mosinet Geremew" ], "Country": [ "Kenya", "Kenya", "Ethiopia", "Ethiopia", "Kenya", "Kenya", "Ethiopia", "Kenya", "Ethiopia", "Ethiopia" ], "Time": [ "2:00:35", "2:01:09", "2:01:41", "2:01:48", "2:02:05", "2:02:16", "2:02:38", "2:02:44", "2:02:48", "2:02:55" ], "Pace_per_Mile": [ "4:36.0", "4:37.2", "4:38.5", "4:38.7", "4:39.4", "4:39.8", "4:40.6", "4:40.8", "4:41.0", "4:41.3" ], "Race": [ "Chicago", "Berlin", "Berlin", "Valencia", "Valencia", "Tokyo", "Valencia", "Chicago", "Berlin", "London" ], "Year": [ 2023, 2022, 2019, 2023, 2024, 2024, 2024, 2024, 2019, 2019 ] }

Pass the dictionary into a dataframe and we should be good to go for the lesson.

  df = pd.DataFrame(data) df.head(10)

Example 1 - Single Row

If we want to grab a single row, pass in the index to iloc. As shown above in the dataframe, unless modified, each row has an index starting with 0. In this case Kelvin Kiptum has an index of 0 although he is in the first row.

  df.iloc[0]

If we grab the iloc[1] we get row 2 which is Eliud Kipchoge.

  df.iloc[1]

Example 2 - Negative Index

While the examples above were positive, we can also use a negative number with iloc. If we use -1 it will grab the last row in the dataframe. This can be helpful when you are working with a ton of data.

  df.iloc[-1]

Example 3 - Multiple Rows

If we want to grab multiple rows, use double brackets and pass in the row numbers.Â

  df.iloc[[0,1]]

The rows do not have to be in order. In this second example you can see we grab the index of 0, 1 and 9.

  df.iloc[[0,1,9]]

Example 4 - single row and a single column

Now let’s add on the complexity of looking at a specific column. How iloc works is [row, column]. In the example below we want to get Kipchoge’s Marathon time. To do that we use index of 1 for the row and 3 for the column. Just like with rows, we start at 0 since the marathon time is the 4th column.

  df.iloc[1, 3]

Example 5 - Multiple rows and single column

We can also look at multiple rows at a time with one column.

  df.iloc[[0,1,9], 3]

Example 6 - Multiple rows and multiple columns

Or we can look at multiple rows and columns at a time.

  df.iloc[[0,1,9], [1, 3]]

Example 7 - Grab column

If we want to grab an entire column, we will need to get every row. This can be achieved by passing in a colon :. While we haven’t covered this yet, it will show up shortly when we talk about slicing.

  df.iloc[:,5]

Example 8 - slicing rows

So back to the colon. Slicing works different whether you use iloc or loc, which can be confusing. But regardless let’s jump into it. [Start (which is included): Stop (which is not included)]

In this first example we start at the index of 0 and stop at 1. So the index of 1 is not included.

  df.iloc[0:1]

When we change the stopping index position to 2, the first index is included and the 2nd is excluded.

  df.iloc[0:2]

Example 9 - slicing columns

We can also use slicing with columns. Just remember for the rows to use a colon if you want to grab all of them. The same start/stop rules apply here.

  df.iloc[:, 1:5]

Example 10 - slicing rows and columns

Lik earlier where we can select multiple rows or columns, we can slice both at the same time.

  df.iloc[0:3, 1:5]

Example 11 - Steps row

While this isn’t super helpful, you can also use steps. This would be after the second colon. Using 2 allows you to skip rows.

  df.iloc[0:9:2, :]

Example 12 - Steps column

The same as above can be applied to columns.

  df.iloc[:, 0:6:2]

Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Leave a Reply

Your email address will not be published. Required fields are marked *