Status
Done
3. Label-Based & Positional Access
Access cells, rows, or slices by label or position.
df.at[row_label, col_label]
→ Fast label-based scalar access (single cell).df.iat[row_index, col_index]
→ Fast position-based scalar access.df.loc[]
→ Select rows/columns by label; supports slices, conditions, or lists.df.iloc[]
→ Select rows/columns by integer position.
Key: at
/iat
→ single cell (fast). loc
/iloc
→ slices or multiple rows/columns.
Core Access Methods Overview
Method | Selection Type | Speed | Use Case |
at | Single cell by label | Fastest | Precise value lookup/update |
iat | Single cell by position | Fastest | Position-based scalar operations |
loc | Multiple items by label | Fast | Label-based slicing/boolean indexing |
iloc | Multiple items by position | Fast | Position-based slicing |
We'll use the Titanic dataset for examples:
python
import pandas as pd
import seaborn as sns
df = sns.load_dataset('titanic').set_index('class')
1. df.at[]
 - Label-Based Scalar Access
DataFrame.at[row_label, column_label]
Key Features
- Fastest method for single cell access
- Works only with existing labels
- Returns a single value (not a Series/DataFrame)
Examples
python
# Get fare for First class passenger at index 1
fare = df.at['First', 'fare'] # Returns scalar value
# Update age of Third class passenger at index 3
df.at['Third', 'age'] = 22 # In-place modification
Professional Usage
python
# Safe access with try-except
try:
value = df.at['Nonexistent', 'column']
except KeyError:
value = None
# Bulk updates with .at
for cls in ['First', 'Second']:
df.at[cls, 'fare'] *= 1.1 # 10% fare increase
2. df.iat[]
 - Position-Based Scalar Access
Syntax
python
DataFrame.iat[row_position, column_position]
Key Features
- Position-based (zero-indexed)
- Faster thanÂ
iloc
 for single cells - No label checking - pure integer positions
Examples
python
# Get value at row 0, column 2
value = df.iat[0, 2] # Returns age of first passenger
# Update value at row 5, column 1
df.iat[5, 1] = 'female' # Change sex of 6th passenger
Performance Comparison
python
%timeit df.at['First', 'fare'] # ~1.5 μs
%timeit df.iat[0, 3] # ~1.3 μs
%timeit df.loc['First', 'fare'] # ~5 μs
3. df.loc[]
 - Label-Based Selection
Syntax
python
DataFrame.loc[row_selection, column_selection]
Selection Types
- Single Label
- List of Labels
- Slice with Labels
- Boolean Masks
python
df.loc['First', 'age'] # Series of ages for First class
python
df.loc[['First', 'Third'], ['age', 'survived']]
python
df.loc['First':'Second', 'age':'fare'] # Inclusive!
python
df.loc[df['age'] > 30, 'survived'] # Survival status for >30
Advanced Usage
python
# Conditional assignment
df.loc[df['age'] < 18, 'category'] = 'child'
# Cross-section with xs
df.loc[df.index.drop_duplicates(), :] # Remove duplicate indices
4. df.iloc[]
 - Position-Based Selection
Syntax
python
DataFrame.iloc[row_positions, column_positions]
Selection Types
- Integer Position
- Lists/Slices
- Boolean Masks
python
df.iloc[5, 2] # 6th row, 3rd column
python
df.iloc[10:15, [0, 2, 4]] # Rows 10-14, specific columns
python
df.iloc[(df['age'].values > 30).nonzero()[0]]
Performance Tip
python
# Faster than loc for position-based operations
%timeit df.iloc[100:200, 3:5] # ~50 μs
%timeit df.loc[df.index[100:200], df.columns[3:5]] # ~150 μs
Professional Access Patterns
1. Safe Access Wrapper
python
def safe_access(df, row, col, accessor='loc', default=None):
try:
if accessor == 'loc':
return df.loc[row, col]
elif accessor == 'iloc':
return df.iloc[row, col]
elif accessor == 'at':
return df.at[row, col]
elif accessor == 'iat':
return df.iat[row, col]
except (KeyError, IndexError):
return default
2. Chained Operation Protection
python
# Dangerous (may cause SettingWithCopyWarning)
df[df['age'] > 30]['fare'] = 100
# Safe using loc
df.loc[df['age'] > 30, 'fare'] = 100
3. Multi-Index Access
python
# Create MultiIndex example
multi_df = df.set_index(['class', 'who'])
# Cross-section access
multi_df.loc[('First', 'man'), :]
# Partial label selection
multi_df.loc[('First', slice(None)), 'age']
Access Method Selection Guide
Scenario | Recommended Method |
Single cell lookup | at /iat |
Conditional row selection | loc |
Column subsetting | loc[:, column_list] |
Position-based slicing | iloc |
Mixed label/position | df.loc[df.index[positions], :] |
Boolean indexing | loc  with condition |
Performance Optimization
- Pre-filter columns before row operations:
- Use numpy arrays for complex operations:
- Avoid chained indexing:
python
cols = ['age', 'fare']
df.loc[df['survived'] == 1, cols]
python
mask = (df['age'].values > 30) & (df['sex'].values == 'male')
df.iloc[mask.nonzero()[0]]
python
# Bad
df['age'][df['sex'] == 'male'] = 30
# Good
df.loc[df['sex'] == 'male', 'age'] = 30
This comprehensive guide provides professional patterns for efficient DataFrame access in real-world data analysis workflows. The key is matching the access method to your specific selection needs while maintaining code clarity and performance.