loc vs iloc
Let me give you the most important distinction in Pandas indexing. `loc` and `iloc` look similar, but they work completely differently.
iloc — Integer Location
Use `iloc` when you want rows by their numerical position. Think of it like "integer location":
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]}
df = pd.DataFrame(data)
print(df.iloc[0])
print(df.iloc[0:2])
`iloc[0]` gets the first row. `iloc[0:2]` gets rows at positions 0 and 1. It's pure position-based — doesn't care about labels at all.
loc — Label Location
Use `loc` when you want rows by their label. This is where it gets interesting:
print(df.loc[0])
print(df.loc[0:2])
Wait, that looks the same, right? Here is the thing — `loc[0:2]` includes row 2 because it's label-based slicing. With `iloc`, the end is excluded. With `loc`, both ends are included.
The Key Difference
One thing that confused me at first was when labels don't match positions. Check this out:
df.index = ['a', 'b', 'c']
print(df.loc['a':'c'])
print(df.iloc[0:2])
`loc` uses the labels 'a', 'b', 'c'. `iloc` uses positions 0, 1. Same DataFrame, different approaches. Use `iloc` for position, `loc` for labels. Simple as that.
Try it Yourself →Key Takeaways
- `iloc` selects by integer position (like array indexing)
- `loc` selects by label (like dictionary keys)
- `iloc` excludes the end index; `loc` includes it
- Use `iloc` for position, `loc` for labels — keep it simple