pbPassingBI
/

What is the difference between loc and iloc?

beginner
Answer

loc selects by label — column names, index values, or a boolean mask. iloc selects by integer position.

A detail worth knowing: loc slices are inclusive of the end value, while iloc slices are exclusive, matching normal Python slicing. df.loc[0:5] returns six rows; df.iloc[0:5] returns five.

In practice loc is what you use most, because filtering with a boolean mask goes through it: df.loc[df['revenue'] > 1000, 'region'].

Related