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

Python | Extracting rows using Pandas .iloc[]

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[]:

1. Setup:

Make sure you have the necessary library:

pip install pandas

2. Import the necessary library:

import pandas as pd

3. Create a Sample DataFrame:

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)

4. Extracting Rows Using .iloc[]:

a. Single Row:

To extract the row with index 2:

row = df.iloc[2]
print(row)

b. Multiple Rows:

To extract rows with index 1 and 3:

rows = df.iloc[[1, 3]]
print(rows)

c. Row Range:

To extract rows from index 1 up to (and excluding) 4:

rows_range = df.iloc[1:4]
print(rows_range)

5. Extracting Rows and Columns Simultaneously:

With .iloc[], you can also specify both row and column indices to extract specific rows and columns.

a. Single Row, Multiple Columns:

Extract the second row and the first two columns:

subset = df.iloc[1, 0:2]
print(subset)

b. Multiple Rows, Multiple Columns:

Extract the first three rows and the first two columns:

subset = df.iloc[0:3, 0:2]
print(subset)

6. Caveats and Tips:

  • 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.

Summary:

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.

  1. Index-based row extraction in Pandas:

    • Rows can be extracted by providing the integer index of the desired row.
    # Extract row at index 2
    selected_row = df.iloc[2]
    
  2. Extracting single row with iloc in Pandas:

    • Use iloc to extract a single row based on its integer index.
    # Extract single row at index 2
    single_row = df.iloc[2]
    
  3. Pandas iloc for extracting multiple rows:

    • Use a list of integer indices to extract multiple rows.
    # Extract multiple rows at indices 1, 3, and 5
    multiple_rows = df.iloc[[1, 3, 5]]
    
  4. Slicing rows with iloc in Pandas DataFrame:

    • Slice rows using integer indices to select a range.
    # Slice rows from index 2 to 5
    sliced_rows = df.iloc[2:6]
    
  5. Selecting rows by position using iloc:

    • Use iloc for selecting rows based on their positional index.
    # Select rows at positions 0, 2, and 4
    selected_rows = df.iloc[[0, 2, 4]]
    
  6. Accessing rows and columns using iloc in Pandas:

    • Combine 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