# What is the Python pandas property iloc\[\]?

The [Python pandas](https://www.ionos.com/digitalguide/websites/web-development/python-pandas/) DataFrame property `iloc[]` is used to select data within a pandas DataFrame **using indices**. This allows you to view specific rows and columns of a DataFrame.

## What is the syntax for pandas `iloc[]`?

Pandas `iloc[]` uses integers to specify **which elements from the DataFrame should be selected**. The general syntax for pandas `DataFrame.iloc()` is:

```python
DataFrame.iloc[selection]
```

You can pass pandas `iloc[]` **a single integer, a [Python list](https://www.ionos.com/digitalguide/websites/web-development/python-list/) of integers, a slice object or a [Python tuple](https://www.ionos.com/digitalguide/websites/web-development/python-tuples/) with row and column indices**.

## How to use pandas `DataFrame.iloc[]`

The behavior of pandas `iloc[]` changes depending on the value you pass to the property. We’ve provided different examples below to help illustrate this.

### Selecting a row

First, we’re going to create a DataFrame with various people, their ages and the cities where they live:

```python
import pandas as pd
# Example of how to create a DataFrame
data = {'Name': ['Alicia', 'Carlos', 'Dara', 'Corey'],
    'Age': [28, 24, 22, 32],
    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']}
df = pd.DataFrame(data)
print(df)
```

The resulting DataFrame looks like this:

```none
Name  Age        	City
0   Alicia   28    	New York
1   Carlos   24  		Los Angeles
2     Dara   22    	Chicago
3    Corey   32     	Houston
```

Using `iloc[]`, you can now select any row by passing the corresponding row index:

```python
# Selecting the first row (index 0)
result = df.iloc[0]
print(result)
```

In this example, the first row (index 0) has been selected. The result contains the data for Alicia:

```none
Name       Alicia
Age            28
City     New York
Name: 0, dtype: object
```

### Selecting a row and a column

If you want to specify both a row and column index, simply pass those values to `iloc[]` with a comma placed between them:

```python
# Select the first row and second column
result = df.iloc[0, 1]
print(result)
```

With the code above, pandas `iloc[]` selects the first row (index 0) and the second column (index 1). The result is Alicia’s age: 28.

### Selecting multiple rows and columns using slices

You can also simultaneously select multiple rows and columns using Python slices. Keep in mind that the index after the colon is not included in the selection.

```python
# Select the first two rows and first two columns
result = df.iloc[0:2, 0:2]
print(result)
```

The output for the above code is:

```none
Name  Age
0  Alicia   28
1  Carlos   24
```

Here, the first two rows (`0:2`) and the first two columns (`0:2`) are selected. The resulting DataFrame only includes the data in rows 0 and 1 and columns 0 and 1.

### Selecting multiple rows and columns with lists

You can also use Python lists to select multiple rows and columns. The benefit of lists is that you can select parts of the DataFrame that aren’t directly next to each other:

```python
# Select the first and third rows and the second and third columns
result = df.iloc[[0, 2], [1, 2]]
print(result)
```

Here, the first and third rows (`[0, 2]`) and the second and third columns (`[1, 2]`) are selected, resulting in the following output:

```none
Age        City
0     28    New York
2     22     Chicago
```


This is a markdown version of: [https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-iloc/](https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-iloc/) for AI/LLM consumption.