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?
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?
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 onlyiloc
- selects subsets of rows and columns by integer location onlyFor more detailed explanation you can check this question