What is difference between loc and iloc in Python?

The main difference between loc and iloc in Pandas is that loc uses the label of the rows and columns to select data, while iloc uses the integer position of the rows and columns.

  • To use loc, you need to specify the rows and columns using their row and column labels.
  • To use iloc, you need to specify the rows and columns using their integer positions, starting from 0 for the first row or column.

Both loc and iloc can be used to select and manipulate data in a Pandas dataframe.

Example