![]() |
VOOZH | about |
When working with labeled data or referencing specific positions in a DataFrame, selecting specific rows and columns from Pandas DataFrame is important. In this article, we’ll focus on pandas functions—loc and iloc—that allow you to select rows and columns either by their labels (names) or their integer positions (indexes).
Let's see an basic example to understand both methods:
Output:
Using loc: Alice
Using iloc: Los Angeles
The .loc[] method selects data based on labels (names of rows or columns). It is flexible and supports various operations like selecting single rows/columns, multiple rows/columns, or specific subsets.
Key Features of .loc[]:
Output:
Name Alice
Age 25
City New York
Name: 0, dtype: object
Output:
Name Age City
0 Alice 25 New York
2 Charlie 35 Chicago
Output:
Name City
0 Alice New York
1 Bob Los Angeles
Output:
Name Age City
1 Bob 30 Los Angeles
2 Charlie 35 Chicago
The .iloc[] method selects data based on integer positions (index numbers). It is particularly useful when you don’t know the labels but know the positions.
Key Features of .iloc[]:
Output:
Name Bob
Age 30
City Los Angeles
Name: 1, dtype: object
Output:
Name Age City
0 Alice 25 New York
2 Charlie 35 Chicago
Output:
Age
0 25
2 35
loc and ilocAlthough both loc and iloc allow row and column selection, they differ in how they handle indexes:
| Feature | loc | iloc |
|---|---|---|
| Indexing Basis | Label-based (uses row/column labels) | Position-based (uses integer positions) |
| Inclusiveness | Inclusive of both start and end points | Exclusive of the end point (slicing) |
| Row Selection | Works with index labels (could be strings) | Works with integer positions (0, 1, 2, ...) |
| Column Selection | Works with column labels (can be strings) | Works with integer positions (0, 1, 2, ...) |
loc when:iloc when:Both loc and iloc are incredibly useful tools for selecting specific data in a Pandas DataFrame. The key difference is whether you're selecting by label (loc) or index position (iloc). Understanding how and when to use these methods is essential for efficient data manipulation.