---
description: How to use iloc and loc for Indexing and Slicing Pandas Dataframe
---

# How to use iloc and loc for Indexing

‌

![](https://www.marsja.se/wp-content/uploads/2019/09/pandas_loc_vs_iloc_tutorial.png)

In this post, we are going to work with Pandas **iloc**, and **loc**. More specifically, we are going to learn slicing and indexing by iloc and loc examples.‌

Once we have a dataset loaded as a Pandas dataframe, we often want to start accessing specific parts of the data based on some criteria. For instance, if our dataset contains the result of an experiment comparing different experimental groups, we may want to calculate descriptive statistics for each experimental group separately.​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/how_does_pandas_loc_work-1024x713.png)

* ​[More about grouping Pandas dataframes](https://www.marsja.se/python-pandas-groupby-tutorial-examples/)​
* ​[How to descriptive statistics in Python](https://www.marsja.se/pandas-python-descriptive-statistics/)​

‌The procedure of selecting specific rows and columns of data based on some criteria is commonly known as _slicing_.‌

**Table of Contents**‌

* ​[Pandas Dataframes](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Pandas_Dataframes)​
  * ​[Data](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Data)​
* ​[What is the Difference Between loc and iloc?](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#What_is_the_Difference_Between_loc_and_iloc)​
* ​[What does iloc do in Pandas?](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#What_does_iloc_do_in_Pandas)​
* ​[How to use Pandas iloc](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#How_to_use_Pandas_iloc)​
  * ​[Pandas iloc Examples](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Pandas_iloc_Examples)​
  * ​[Selecting Columns with Pandas iloc](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Selecting_Columns_with_Pandas_iloc)​
  * ​[Select a Specific Cell using iloc](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Select_a_Specific_Cell_using_iloc)​
  * ​[Retrieving subsets of cells](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Retrieving_subsets_of_cells)​
  * ​[Selecting Columns using a Boolean Mask](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Selecting_Columns_using_a_Boolean_Mask)​
* ​[How to Use Pandas loc](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#How_to_Use_Pandas_loc)​
  * ​[When to use loc?](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#When_to_use_loc)​
  * ​[Select a Row using Pandas loc](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Select_a_Row_using_Pandas_loc)​
  * ​[Slicing Rows using loc](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Slicing_Rows_using_loc)​
  * ​[Selecting by Column Names using loc](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Selecting_by_Column_Names_using_loc)​
  * ​[Slicing using loc in Pandas](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Slicing_using_loc_in_Pandas)​
* ​[Pandas iloc and Conditions](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Pandas_iloc_and_Conditions)​
  * ​[Select Rows using Multiple Conditions Pandas iloc](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Select_Rows_using_Multiple_Conditions_Pandas_iloc)​
* ​[Setting Values in dataframes using .loc](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Setting_Values_in_dataframes_using_loc)​
* ​[Conclusion](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#Conclusion)​

‌

### **Pandas Dataframes**‌ <a href="#pandas-dataframes" id="pandas-dataframes"></a>

Before we are going to learn how to work with loc and iloc, we are it can be good to have a reminder on how Pandas dataframe object work. For the specific purpose of this indexing and slicing tutorial it is good to know that each row and column, in the dataframe, has a number – an index.​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/pandas_what_does_iloc_do.png)

This structure, a row-and-column structure with numeric indexes, means that we can work with data by using the row and the column numbers. This is useful to know when we are going to work with Pandas loc and iloc methods.‌

* See also the blog post [using Pandas and pyjanitor, to learn about some easy Python data cleansing methods.](https://www.marsja.se/easiest-data-cleaning-method-using-python-pandas-pyjanitor/)​

‌

#### Data‌ <a href="#data" id="data"></a>

In the following iloc and loc example we are going to work with two datasets. These datasets, among a lot of other RDatasets, can be found here but the following code will load them into Pandas dataframes:

```python
import pandas as pd​
url_dataset1 = 'https://vincentarelbundock.github.io/Rdatasets/csv/psych/affect.csv'
url_dataset2 = 'https://vincentarelbundock.github.io/Rdatasets/csv/DAAG/nasshead.csv'
df1 = pd.read_csv(url_dataset1, index_col=0)
df2 = pd.read_csv(url_dataset2, index_col=0)
```

‌

If you are interested in learning more about in-and-out methods of Pandas make sure to check the following posts out:‌

* ​[How to read CSV files in Pandas](https://www.marsja.se/pandas-read-csv-tutorial-to-csv/)​
* ​[How to read Excel files in Pandas](https://www.marsja.se/pandas-excel-tutorial-how-to-read-and-write-excel-files/)​
* ​[Reading SPSS files in Pandas](https://www.marsja.se/how-to-read-write-spss-files-in-python-pandas/)​
* ​[Working with JSON files using Python and Pandas](https://www.marsja.se/how-to-read-and-write-json-files-using-python-and-pandas/)​

‌

Before going on and working with Pandas iloc and Pandas loc, we will answer the question concerning the difference between loc and iloc.‌

### What is the Difference Between loc and iloc?‌ <a href="#what-is-the-difference-between-loc-and-iloc" id="what-is-the-difference-between-loc-and-iloc"></a>

First of all, .loc is a label based method whereas .iloc is an integer-based method. This means that iloc will consider the _names_ or labels of the index when we are slicing the dataframe. For example, if “case” would be in the index of a dataframe (e.g., _df_), `df.loc['case']` will result in that the third row is being selected. Note, in the loc and iloc examples below we will work with the first column, in the dataset, as index (see first code chunk). On the other hand, Pandas .iloc takes slices based on index’s position. Unlike .loc, .iloc behaves like [regular Python slicing](https://en.wikipedia.org/wiki/Array_slicing#1991:_Python). That is, we just indicate the positional index number, and we get the slice we want. For example, df.iloc\[2] will give us the third row of the dataframe. This is because, just like in Python, .iloc is zero positional based. That is it starts at 0. We will learn how we use loc and iloc, in the following sections of this post.‌

### What does iloc do in Pandas?‌ <a href="#what-does-iloc-do-in-pandas" id="what-does-iloc-do-in-pandas"></a>

As previously mentioned, Pandas iloc is primarily integer position based. That is, it can be used to index a dataframe using 0 to length-1 whether it’s the row or column indices. Furthermore, as we will see in a later Pandas iloc example, the method can also be used with a boolean array.​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/slicing_rows_using_pandas_iloc.png)

In the image below, we can see a quick example of how to work with Pandas df.loc\[] method.​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/pandas_dataframe_loc_indexing_by_name.png)

​[Link to the Pandas loc example](https://gist.github.com/marsja/074ed7e1d2f0cd1cc3a35723280c7c7a)​‌

Here’s a short YouTube video in which you can learn how to work with both loc and iloc:‌

In this Pandas iloc tutorial, we are going to work with the following input methods:‌

* An integer, e.g. 2
* A list of integers, e.g. \[7, 2, 0]
* A slice object with ints, e.g. 0:7, as in the image above
* A boolean array.

### ‌How to use Pandas iloc

Now you may be wondering “how do I use iloc?” and we are, of course, going to answer that question. In the simplest form we just type an integer between the brackets.

```python
df1.iloc[0]​‌
```

As can be seen in the Pandas iloc example, above, we typed a set of brackets after the iloc method.‌

Furthermore, we added an integer (0) as index value to specify that we wanted the first row of our dataframe. Note, when working with .iloc it’s important to know that order of the indexes inside the brackets obviously matters.‌

The first index number will be the row or rows that we want to retrieve. If we wat to retrieve a specific column, or specific columns, using iloc we input a second index (or indices). This, however, is _optional_ and without a second index, iloc will retrieve all columns by default.‌

Pandas iloc syntax is, as previously described, DataFrame.iloc\[\<row selection>, \<column selection>].​![](https://www.marsja.se/wp-content/uploads/2019/09/pandas_select_row_by_index_iloc.png)‌

This may be confusing for users of the R statistical programming environment. To iterate, the iloc method in Pandas is used to **select rows and columns by number**, in the order that they _appear_ in the dataframe.‌

#### Pandas iloc Examples‌ <a href="#pandas-iloc-examples" id="pandas-iloc-examples"></a>

In the next section, we continue this Pandas indexing and slicing tutorial by looking at different examples of how to use iloc. We have, of course, already started with the most basic one; selecting a single row:

```python
df1.iloc[3]
```

‌**Indexing the last Row of a Pandas dataframe**‌

In the next example, we are continuing using one integer to index the dataframe. However. if we want to retrieve the last row of a Pandas dataframe we use “-1”:

```python
df1.iloc[-1]
```

‌We can also input a list, with only one index integer, when we use iloc. This will index one row but the output will be different compared to the example above:

```python
df1.iloc[[-1]]
```

​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/pandas_get_the_last_row_iloc.png)

**pSelect Multiple Rows using iloc**‌

We can, of course, also use iloc to select many rows from a pandas dataframe. For instance, if we add more index integers to the list, like in the example above, we can select many rows.

```python
df.iloc[[7, 2, 0]]
```

​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/iloc_vs_loc_indexing_many_rows.png)

**Slicing Rows using iloc in Pandas**‌

In the next Pandas iloc example, we are going to learn about slicing. Note, we are going to get more familiar using the slicing character “:” later in this post. To select row 11 to 15 we type the following code:

```python
df1.iloc[10:15]
```

​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/slicing_rows_iloc_pandas_tutorial.png)

#### Selecting Columns with Pandas iloc‌ <a href="#selecting-columns-with-pandas-iloc" id="selecting-columns-with-pandas-iloc"></a>

As previously indicated, we can, of course, when using the second argument in the iloc method also select, or slice, columns. In the next iloc example, we may want to retrieve only the first column of the dataframe, which is the column at index position 0.‌

To do this, we will use an integer index value in the _second_ position inside of the brackets when we use iloc. Note, the integer index in the second position specifies the _column_ that we want to retrieve. What about the rows?​![](https://www.marsja.se/wp-content/uploads/2019/09/iloc_select_all_rows_and_first_column_in_pandas.png)‌

Note, that when we want to select all rows and one column (or many columns) using iloc we need to use the “:” character.

```python
df1.iloc[:, 0]
```

​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/indexing_all_rows_and_one_column_iloc_pandas.png)

In the Pandas iloc example above, we used the “:” character in the first position inside of the brackets. This indicates that we want to retrieve all the rows. A reminder; the first index position inside of `[]`, specifies the rows, and we used the “:” character, because we wanted to get all rows from a Pandas dataframe.‌

In the next example of how to use Pandas iloc, we are going to take a slice of the columns and all rows. This can be done in a similar way as above. However, instead of using an integer we use a Python slice to get all rows and the first 6 columns:

```
df1.iloc[:, 0:6]
```

​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/slicing_columns_getting_all_rows_iloc_pandas.png)

#### Select a Specific Cell using iloc‌ <a href="#select-a-specific-cell-using-iloc" id="select-a-specific-cell-using-iloc"></a>

In this section, of the Pandas iloc tutorial, we will learn how to select a specific cell.‌

This is quite simple, of course, and we just use an integer index value for the row and for the column we want to get from the dataframe. For example, if we want to select the data in row 0 and column 0, we just type `df1.iloc[0, 0]`.​![](https://www.marsja.se/wp-content/uploads/2019/09/pandas_iloc_to_string.png)‌

Of course, we can also select multiple rows and/or multiple columns. To do this we just add a list with the integer indices that we want iloc to select for us.‌

For example, if we want to select the data in row 4 and column 2, 3, and 4 we just use the following code:

```python
df1.iloc[3, [1, 2, 3]]
```

‌

#### Retrieving subsets of _cell&#x73;_&#x200C; <a href="#retrieving-subsets-of-cells" id="retrieving-subsets-of-cells"></a>

In the next iloc example, we are going to get a subset of cells from the dataframe.‌

Achieving this is a combination of getting a slice of columns and a slice of rows with iloc:

```
df1.iloc[0:5, 3:7]
```

‌

#### Selecting Columns using a Boolean Mask‌ <a href="#selecting-columns-using-a-boolean-mask" id="selecting-columns-using-a-boolean-mask"></a>

In the final example, we are going to select columns using a boolean mask. Doing this, of course, require us to know how many columns there are and which columns we want to select.

```python
bool_i = [False, False, True, True, True,  True, True, True, True, True,  True, True, True, True, True, True, True, True, False, False]​df.iloc[3:7, bool_i]
```

​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/how_to_use_iloc_and_loc_slicing.png)

### How to Use Pandas loc‌ <a href="#how-to-use-pandas-loc" id="how-to-use-pandas-loc"></a>

In this section, we are covering another Pandas method, i.e. loc for selecting data from dataframes.​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/loc_vs_iloc_pandas_python_dataframe_indexing.png)

#### When to use loc? <a href="#when-to-use-loc" id="when-to-use-loc"></a>

Remember, whereas iloc takes the positional references as the argument input while loc takes indexes as the argument. As loc takes indexes, we can pass strings (e.g., column names) as an argument whereas it will throw an error if we used strings with iloc. So the answer to the question _when to use Pandas loc_? is when we know the index names.‌

In this loc tutorial we are going to use the following inputs:‌

* A single label, for instance 2 or ‘b’. It is worth noting here that Pandas interpret 2 as a label of the index and not as an integer position along the index (contrary to iloc)
* A list of labels, for instance \[‘a’, ‘b’, c’]
* A slice object with labels, for example,. ‘shortname’:’SASname’. **Importantly**, when it comes to slices when we use loc, **BOTH** the start and stop are included‌

#### Select a Row using Pandas loc‌ <a href="#select-a-row-using-pandas-loc" id="select-a-row-using-pandas-loc"></a>

In the first Pandas loc example, we are going to select data from the row where the index is equal to 1.

```python
df2.loc[1]
```

‌Note, in the example above the first row has the name “1”. That is, this is not the index integer but the name.‌

Pandas loc behaves the in the same manner as iloc and we retrieve a single row as series. Just as with Pandas iloc, we can change the output so that we get a single row as a dataframe. We do this by putting in the row name in a list:

```python
df2.loc[[1]]
```

​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/pandas_loc_row_to_dataframe.png)

#### Slicing Rows using loc‌ <a href="#slicing-rows-using-loc" id="slicing-rows-using-loc"></a>

In the next code example, we are going to take a slice of rows using the row names.

```python
df2.loc[1:5]
```

​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/loc_pandas_slicing_rows.png)

We can also pass it a list of indexes to select required indexes.

```python
df2.loc[[1, 6, 11, 21, 51]]
```

​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/loc_in_pandas_list_of_row_names.png)

#### Selecting by Column Names using loc‌ <a href="#selecting-by-column-names-using-loc" id="selecting-by-column-names-using-loc"></a>

Unlike Pandas iloc, loc further takes column names as column argument. This means that we can pass it a column name to select data from that column.‌

In the next loc example, we are going to select all the data from the ‘SASname’ column.

```python
df2.loc[:, 'SASname']
```

‌Another option is, of course, to pass multiple column names in a list when using loc. In the next example, we are selecting data from the ‘SASname’, and ‘longname’ columns where the row names are from 1 to 5.

```python
df2.loc[1:5, ['SASname', 'longname']]
```

​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/select_by_column_names_loc_pandas.png)

#### Slicing using loc in Pandas‌ <a href="#slicing-using-loc-in-pandas" id="slicing-using-loc-in-pandas"></a>

In this section, we will see how we can slice a Pandas dataframe using loc. Remember that the “:” character is used when slicing. As with iloc, we can also slice but here we can column names and row names (like in the example below).‌

In the loc example below, we use the first dataframe again (df1, that is) and slice the first 5 rows and take the columns from the ‘Film’ column to the ‘EA1’ column

```
df1.loc[1:5, 'Film':'EA1']
```

### ‌Pandas iloc and Conditions‌

Many times we want to index a Pandas dataframe by using boolean arrays. That is, we may want to select data based on certain conditions. This is quite easy to do with Pandas loc, of course. We just pass an array or Seris of True/False values to the .loc method.‌

For example, if we want to select all rows where the value in the _Study_ column is “flat” we do as follows to create a **Pandas Series** with a True value for every row in the dataframe, where “flat” exists.

```python
df1.loc[df1['Study'] == 'flat']
```

​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/conditional_selection_of_rows_loc_pandas.png)

#### Select Rows using Multiple Conditions Pandas iloc‌ <a href="#select-rows-using-multiple-conditions-pandas-iloc" id="select-rows-using-multiple-conditions-pandas-iloc"></a>

Furthermore, some times we may want to select based on more than one condition. For instance, if we want to select all rows where the value in the _Study_ column is “flat” and the value in the _neur_ column is larger than 18 we do as in the next example:

```python
df1.loc[(df1['neur'] > 18) & (df1['Study'] == 'flat')]
```

​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/multiple_conditons_selecting_rows_pandas_loc.png)

As before, we can use a second to select particular columns out of the dataframe. Remember, when working with Pandas loc, columns are referred to by name for the loc indexer and we can use a single string, a list of columns, or a slice “:” operation. In the next example, we select the columns from EA1 to NA2:

```python
df1.loc[(df1['neur'] > 18) & (df1['Study'] == 'flat'), 'EA1':'NA2']
```

​‌

![](https://www.marsja.se/wp-content/uploads/2019/09/pandas_loc_indexing_and_slicing_based_on_two_conditions.png)

### Setting Values in dataframes using .loc <a href="#setting-values-in-dataframes-using-loc" id="setting-values-in-dataframes-using-loc"></a>

In the last section, of this loc and iloc tutorial, we are going to learn how to set values to the dataframe using loc.‌

Setting values to a dataframe is easy all we need is to change the syntax a bit, and we can actually update the data in the same statement as we select and filter using .loc indexer. This is handy as we can to update values in columns depending on different conditions.‌

In the final loc example, we are going to create a new column (NewCol) and add the word “BIG” there in the rows where neur is larger than 18:

```python
df1.loc[df1['neur'] > 18, 'NewCol'] = 'BIG'df1.loc[df1['neur'] > 18, 'EA1':'NewCol'].head()
```

### ‌Conclusion‌

In this Pandas iloc and loc tutorial we have learned indexing, selecting, and subsetting using the loc and iloc methods. More specifically, we have learned how these methods work. When it comes to loc we have learned how to select based on conditional statements (e.g., larger than or equal to) as well as that we have learned how to set values using loc.

{% hint style="info" %}
[**Source :**](https://www.marsja.se/how-to-use-iloc-and-loc-for-indexing-and-slicing-pandas-dataframes/#How_to_use_Pandas_iloc)
{% endhint %}

\
