Understanding .iloc
.iloc
stands for “integer location” and allows you to access data within a DataFrame
using integer-based indexing. Unlike .loc
, which uses labels, .iloc
uses numerical positions to select rows and columns. This makes it particularly useful when you know the exact row and column numbers you need. It’s zero-based indexing, meaning the first row is at index 0, the second at index 1, and so on.
Basic Usage: Selecting Single Elements
The simplest application of .iloc
is selecting a single element. Let’s create a sample DataFrame:
import pandas as pd
= {'col1': [1, 2, 3], 'col2': [4, 5, 6], 'col3': [7, 8, 9]}
data = pd.DataFrame(data)
df print(df)
To access the element in the first row (index 0) and second column (index 1), we use:
= df.iloc[0, 1]
element print(element) # Output: 4
Selecting Rows and Columns: Slicing
.iloc
excels at selecting ranges of rows and columns using slicing:
= df.iloc[:2, :]
first_two_rows print(first_two_rows)
= df.iloc[:, :2]
first_two_cols print(first_two_cols)
= df.iloc[1:3, [0, 2]]
specific_selection print(specific_selection)
This demonstrates the flexibility of using slices (:
) to specify ranges. Remember that the upper bound of the slice is exclusive.
Selecting Specific Rows and Columns with Lists
You can also select specific rows and columns by passing lists of integer indices to .iloc
:
= df.iloc[[0, 2], [1, 2]]
selected_rows_cols print(selected_rows_cols)
This method allows for non-contiguous selections, offering greater precision.
Handling Multiple Conditions
While .iloc
doesn’t directly support boolean indexing like .loc
, you can combine it with other Pandas operations to achieve conditional selections. For instance:
= df['col1'] > 1
rows_to_select = df[rows_to_select].iloc[:, 1:3]
selected_data print(selected_data)
This example first filters the DataFrame based on a condition and then uses .iloc
to select specific columns from the filtered result.
Working with Single Rows and Columns
Accessing entire rows or columns is straightforward:
= df.iloc[0]
first_row print(first_row)
= df.iloc[:, 1]
second_col print(second_col)
This simplifies the retrieval of complete rows or columns based on their integer positions.
Modifying Data with .iloc
.iloc
isn’t just for reading; you can also modify the DataFrame using it:
1, 0] = 10
df.iloc[print(df)
#Modify a whole column
0] = [100, 200, 300]
df.iloc[:,print(df)
This allows for in-place updates to specific elements, rows or columns. Remember to be cautious when modifying DataFrames, always back up your data if necessary!