0

If I have a data frame df and want to select multiple rows.

Using .iloc I can do df.iloc[1:8], but doing the same using .loc requires either doing

df.loc[1:8]

or

df.loc[[1,2,3,4,5,6,7,]]

I am curious to know about the reasoning behind the double brackets for the. loc

Gonzalo
  • 752
  • 8
  • 23
  • posible duplicated of : https://stackoverflow.com/questions/33417991/pandas-why-are-double-brackets-needed-to-select-column-after-boolean-indexing – Gonzalo Aug 15 '23 at 16:55
  • Does this answer your question? [Pandas: Why are double brackets needed to select column after boolean indexing](https://stackoverflow.com/questions/33417991/pandas-why-are-double-brackets-needed-to-select-column-after-boolean-indexing) – Gonzalo Aug 15 '23 at 16:57

1 Answers1

2

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.
To answer your question: the arguements of .loc are

  • row label
  • list of row labels : (double brackets) means that you can pass the list of rows when you need to work with specific set of rows.
    example. you will have row indices when you use cross validation techniques.
Sauron
  • 551
  • 2
  • 11