In pandas, loc[]
and iloc[]
are both used to access elements of a DataFrame, but they differ in how they index the rows and columns.
1. loc[]
– Label-based indexing:
loc[]
is used for label-based indexing, meaning you refer to rows and columns by their labels (the index names or column names).- Both rows and columns are accessed using their labels.
- It includes both the start and end labels when slicing.
Example:
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6]
}, index=['row1', 'row2', 'row3'])
# Access by label
df.loc['row1', 'A'] # Returns 1 (value in row1, column 'A')
df.loc['row1':'row2', 'A'] # Slices rows 'row1' to 'row2', column 'A'
2. iloc[]
– Integer-location based indexing:
iloc[]
is used for integer-location based indexing, meaning you refer to rows and columns by their integer positions (i.e., 0-based index).- It works with positional indexing, so you use integer indices for both rows and columns.
- When slicing, it excludes the end position.
Example:
# Access by position (0-based index)
df.iloc[0, 0] # Returns 1 (first row, first column)
df.iloc[0:2, 0] # Slices rows 0 to 1, column 0 (does not include row 2)
Summary:
loc[]
: Accesses data using labels (index and column names), and includes the end index in slicing.iloc[]
: Accesses data using integer positions, and excludes the end position in slicing.