.iloc and .loc are both accessors used in pandas for selecting data from a DataFrame, but they differ in how they reference the data:
.iloc:
- Primarily integer-based indexing.
- Uses integer positions to select rows and columns.
- Syntax:
df.iloc[row_index, column_index] - Example:
df.iloc[0:5, 1:3]selects the first 5 rows and the 2nd and 3rd columns.
.loc:
- Label-based indexing.
- Uses the actual labels of the rows and columns to select data.
- Syntax:
df.loc[row_label, column_label] - Example:
df.loc[0:4, 'Column1':'Column3']selects rows with labels from 0 to 4 and columns from 'Column1' to 'Column3'.
In summary, use .iloc for positional indexing and .loc for label-based indexing.
