We use loc and iloc to pick specific rows from a table of data. This helps us look at or work with just the parts we want.
0
0
Selecting rows (loc, iloc) in Data Analysis Python
Introduction
You want to see data for a specific date or label in a table.
You need to grab rows by their position number, like the first 5 rows.
You want to filter rows based on row names or indexes.
You want to select rows between two labels or positions.
You want to quickly check or analyze a small part of a big dataset.
Syntax
Data Analysis Python
df.loc[row_label] df.loc[start_label:end_label] df.iloc[row_position] df.iloc[start_pos:end_pos]
loc uses the row labels or names to select rows.
iloc uses the row numbers (starting at 0) to select rows.
Examples
Selects the row with label '2023-01-01'. Useful if your rows are dates or named.
Data Analysis Python
df.loc['2023-01-01']Selects rows from '2023-01-01' up to '2023-01-03' inclusive.
Data Analysis Python
df.loc['2023-01-01':'2023-01-03']
Selects the very first row by position number 0.
Data Analysis Python
df.iloc[0]Selects the first three rows by position numbers 0, 1, and 2.
Data Analysis Python
df.iloc[0:3]
Sample Program
This code creates a table with sales data for 5 days. It shows how to pick rows by date labels with loc and by position with iloc.
Data Analysis Python
import pandas as pd # Create a simple DataFrame with dates as index data = {'Sales': [100, 150, 200, 130, 170]} dates = pd.date_range('2023-01-01', periods=5) df = pd.DataFrame(data, index=dates) print('Original DataFrame:') print(df) # Select row by label using loc print('\nRow for 2023-01-03 using loc:') print(df.loc['2023-01-03']) # Select rows by label range using loc print('\nRows from 2023-01-02 to 2023-01-04 using loc:') print(df.loc['2023-01-02':'2023-01-04']) # Select row by position using iloc print('\nFirst row using iloc:') print(df.iloc[0]) # Select first three rows using iloc print('\nFirst three rows using iloc:') print(df.iloc[0:3])
OutputSuccess
Important Notes
loc includes the last label in a slice, but iloc does not include the last position.
If your DataFrame index is not labeled, loc may not work as expected; use iloc for position-based selection.
Both loc and iloc can select multiple rows and columns by adding a comma, e.g., df.loc['2023-01-01':'2023-01-03', 'Sales'].
Summary
loc selects rows by their labels or names.
iloc selects rows by their position numbers, starting at zero.
Use these to easily pick the exact rows you want from your data.