Pandas Tutorial
Creating Objects
Viewing Data
Selection
Manipulating Data
Grouping Data
Merging, Joining and Concatenating
Working with Date and Time
Working With Text Data
Working with CSV and Excel files
Operations
Visualization
Applications and Projects
The .iloc[]
indexer in pandas is used to select rows and columns by their integer index. It's purely integer-location based indexing, meaning you'll be using integers to specify the location.
Here's a tutorial on how to extract rows using .iloc[]
:
Make sure you have the necessary library:
pip install pandas
import pandas as pd
For this tutorial, let's use a sample DataFrame:
data = { 'A': [1, 2, 3, 4, 5], 'B': [10, 20, 30, 40, 50], 'C': ['p', 'q', 'r', 's', 't'] } df = pd.DataFrame(data) print(df)
.iloc[]
:To extract the row with index 2:
row = df.iloc[2] print(row)
To extract rows with index 1 and 3:
rows = df.iloc[[1, 3]] print(rows)
To extract rows from index 1 up to (and excluding) 4:
rows_range = df.iloc[1:4] print(rows_range)
With .iloc[]
, you can also specify both row and column indices to extract specific rows and columns.
Extract the second row and the first two columns:
subset = df.iloc[1, 0:2] print(subset)
Extract the first three rows and the first two columns:
subset = df.iloc[0:3, 0:2] print(subset)
Remember that Python uses 0-based indexing. So, the first row is indexed as 0, the second as 1, and so on.
When specifying a range, the ending index is exclusive. For instance, df.iloc[1:4]
will extract rows with indices 1, 2, and 3, but not 4.
Be cautious while using .iloc[]
since it relies on integer indices. If the DataFrame has been modified, shuffled, or rows have been removed, the integer index might not correspond to the original row order.
The .iloc[]
indexer in pandas is a powerful tool for extracting rows and columns based on their integer location. It provides a variety of ways to extract data, from single rows/columns to ranges and specific lists of indices. However, always remember it's purely integer-location based, so make sure to account for the actual order of the DataFrame.
Index-based row extraction in Pandas:
# Extract row at index 2 selected_row = df.iloc[2]
Extracting single row with iloc in Pandas:
iloc
to extract a single row based on its integer index.# Extract single row at index 2 single_row = df.iloc[2]
Pandas iloc for extracting multiple rows:
# Extract multiple rows at indices 1, 3, and 5 multiple_rows = df.iloc[[1, 3, 5]]
Slicing rows with iloc in Pandas DataFrame:
# Slice rows from index 2 to 5 sliced_rows = df.iloc[2:6]
Selecting rows by position using iloc:
iloc
for selecting rows based on their positional index.# Select rows at positions 0, 2, and 4 selected_rows = df.iloc[[0, 2, 4]]
Accessing rows and columns using iloc in Pandas:
iloc
for both rows and columns by providing integer indices for both.# Accessing specific row and column value = df.iloc[2, 1] # Row at index 2, column at index 1