0

In pandas, if i have a dataframe , i can subset it like:

df[df.col == some_condition]

Also, i can do:

df.loc[df.col == some_condition]

What is the difference between the two? The ‘loc’ approach seems more verbose?

Victor
  • 15,903
  • 67
  • 211
  • 390
  • 1
    Does this answer your question? [Python: Pandas Series - Why use loc?](https://stackoverflow.com/questions/38886080/python-pandas-series-why-use-loc) – Shubham Periwal May 11 '21 at 03:15

1 Answers1

2

In simple words:

There are three primary indexers for pandas. We have the indexing operator itself (the brackets []), .loc, and .iloc. Let's summarize them:

  • [] - Primarily selects subsets of columns, but can select rows as well. Can't simultaneously select rows and columns.
  • .loc - selects subsets of rows and columns by label only
  • .iloc - selects subsets of rows and columns by integer location only

For more detailed explanation you can check this question

Anurag Dabas
  • 23,002
  • 8
  • 19
  • 34