Javatpoint Logo
Javatpoint Logo

Pandas loc vs. iloc

The Pandas offers .loc[] and .iloc[] methods for data slicing. Data Slicing generally refers to inspect your data sets. These two methods belong to the index selection method that is used to set an identifier for each row of the data set. The indexing can take specific labels, and these labels can either be an integer or any other value specified by the user.

The .loc[] method is used to retrieve the group of rows and columns by labels or a boolean array present in the DataFrame. It takes only index labels, and if it exists in the caller DataFrame, it returns the rows, columns, or DataFrame. It is a label-based method but may be used with the boolean array.

Whereas, the .iloc[] method is used when the index label of the DataFrame is other than numeric series of 0,1,2,....,n, or in the case when the user does not know the index label.

There are some differences between the above methods, which are given below:

  1. The .loc[] method is a label based method that means it takes names or labels of the index when taking the slices, whereas .iloc[] method is based on the index's position. It behaves like a regular slicing where we just have to indicate the positional index number and simply get the appropriate slice.
  2. The .loc[] method includes the last element of the table whereas .iloc[] method does not include the last element.
  3. The .loc[] method is a name-based indexing, whereas the .iloc[] method is positional based indexing.
  4. The arguments of .iloc[] can be:
    • list of rows and columns
    • range of rows and columns
    • single row and column
    Whereas, the arguments of .loc[] can be:
    • row label
    • list of row label
  5. The .loc[] method indexer can perform the boolean selection by passing the boolean series, but in the case of .iloc[]method, we cannot pass a boolean series.

Next TopicPandas Cheat Sheet





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA