import pandas as pd
import numpy as npData Manipulation with loc and iloc
.loc and .iloc. Each section explains a different selection technique.
Import Libraries
Import Pandas and NumPy for data manipulation.
Create DataFrame with Custom Index
Create a DataFrame with columns A, B, C and custom row labels (‘a’, ‘b’, ‘c’, ‘d’, ‘e’).
data = {'A': [1,2,3,4,5],
'B': [6,7,8,9,10],
'C': [11,12,13,14,15]
}
df = pd.DataFrame(data, index=['a','b','c','d','e'])
df| A | B | C | |
|---|---|---|---|
| a | 1 | 6 | 11 |
| b | 2 | 7 | 12 |
| c | 3 | 8 | 13 |
| d | 4 | 9 | 14 |
| e | 5 | 10 | 15 |
Select Rows by Label Range with loc
Use df.loc["a":'c'] to select rows from label ‘a’ to ‘c’ (inclusive).
df.loc["a":'c']| A | B | C | |
|---|---|---|---|
| a | 1 | 6 | 11 |
| b | 2 | 7 | 12 |
| c | 3 | 8 | 13 |
Select Specific Rows by Label with loc
Use df.loc[['a','c']] to select rows with labels ‘a’ and ‘c’.
df.loc[['a','c']]| A | B | C | |
|---|---|---|---|
| a | 1 | 6 | 11 |
| c | 3 | 8 | 13 |
Select Specific Rows and Columns by Label with loc
Use df.loc[['a','c'],['A','C']] to select rows ‘a’ and ‘c’ and columns ‘A’ and ‘C’.
df.loc[['a','c'],['A','C']]| A | C | |
|---|---|---|
| a | 1 | 11 |
| c | 3 | 13 |
Display the DataFrame
Show the entire DataFrame for reference.
df| A | B | C | |
|---|---|---|---|
| a | 1 | 6 | 11 |
| b | 2 | 7 | 12 |
| c | 3 | 8 | 13 |
| d | 4 | 9 | 14 |
| e | 5 | 10 | 15 |
Select Row by Integer Position with iloc
Use df.iloc[0] to select the first row by its integer position.
df.iloc[0]A 1
B 6
C 11
Name: a, dtype: int64
Select Multiple Rows by Integer Range with iloc
Use df.iloc[0:3] to select rows from position 0 to 2 (Python slicing is exclusive of the end).
df.iloc[0:3]| A | B | C | |
|---|---|---|---|
| a | 1 | 6 | 11 |
| b | 2 | 7 | 12 |
| c | 3 | 8 | 13 |
Select Rows from a Position to End with iloc
Use df.iloc[3:] to select all rows from position 3 to the end.
df.iloc[3:]| A | B | C | |
|---|---|---|---|
| d | 4 | 9 | 14 |
| e | 5 | 10 | 15 |
Select Specific Rows by Integer Position with iloc
Use df.iloc[[0,3]] to select rows at positions 0 and 3.
df.iloc[[0,3]]| A | B | C | |
|---|---|---|---|
| a | 1 | 6 | 11 |
| d | 4 | 9 | 14 |
Select Specific Rows and Columns by Integer Position with iloc
Use df.iloc[[0,3],[0,2]] to select rows at positions 0 and 3, and columns at positions 0 and 2.
df.iloc[[0,3],[0,2]]| A | C | |
|---|---|---|
| a | 1 | 11 |
| d | 4 | 14 |