What is the Python pandas property iloc[]?
The 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.
- 99.9% uptime and super-fast loading
- Advanced security features
- Domain and email included
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:
DataFrame.iloc[selection]
pythonYou can pass pandas iloc[]
a single integer, a Python list of integers, a slice object or a Python tuple with row and column indices.
How to use pandas DataFrame.iloc[]
The behaviour 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:
import pandas as pd
# Example of how to create a DataFrame
data = {'Name': ['Alicia', 'Carlos', 'Dara', 'Craig'],
'Age': [28, 24, 22, 32],
'City': ['Nottingham', 'London', 'Cardiff', 'Hull']}
df = pd.DataFrame(data)
print(df)
pythonThe resulting DataFrame looks like this:
Name Age City
0 Alicia 28 Nottingham
1 Carlos 24 London
2 Dara 22 Cardiff
3 Craig 32 Hull
Using iloc[]
, you can now select any row by passing the corresponding row index:
# Selecting the first row (index 0)
result = df.iloc[0]
print(result)
pythonIn this example, the first row (index 0) has been selected. The result contains the data for Alicia:
Name Alicia
Age 28
City Nottingham
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:
# Select the first row and second column
result = df.iloc[0, 1]
print(result)
pythonWith 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.
# Select the first two rows and first two columns
result = df.iloc[0:2, 0:2]
print(result)
pythonThe output for the above code is:
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:
# Select the first and third rows and the second and third columns
result = df.iloc[[0, 2], [1, 2]]
print(result)
pythonHere, the first and third rows ([0, 2]
) and the second and third columns ([1, 2]
) are selected, resulting in the following output:
Age City
0 28 Nottingham
2 22 Cardiff