0

Both the following lines seem to give the same output:

df1 = df[df['MRP'] > 1500]
df1 = df.loc[df['MRP'] > 1500]

Is loc an optional attribute when searching dataframe?

variable
  • 8,262
  • 9
  • 95
  • 215
  • What do you mean "Is loc an optional attribute when searching dataframe"? In both examples you're either _filtering_ or _selecting_ from a dataframe. The "search" occurs in the creation of the Boolean Index `df['MRP'] > 1500`. If the question is, is `loc` optional when selecting data from a DataFrame the answer to that is, of course, yes and no. There is flexibility in `loc` that you won't find from standard `__getitem__` but there is significant overlap on simple conditions. – Henry Ecker Jun 09 '21 at 11:54
  • `loc` gives you some extra functionalities, for example selecting certain columns: `df.loc[df['MRP'] > 1500, ['ColA', 'ColB']]`. Or if you want to assign values in a certain column after selecting: `df.loc[df['MRP'] > 1500, 'ColA'] = 10` – Erfan Jun 09 '21 at 12:06

1 Answers1

1

Coming from Padas.DataFrame.loc documentation:

Access a group of rows and columns by label(s) or a boolean array.

.loc[] is primarily label based, but may also be used with a boolean array.

When you are using Boolean array to filter out data, .loc is optional, and in your example df['MRP'] > 1500 gives a Series with the values of truthfulness, so it's not necessary to use .loc in that case.

df[df['MRP']>15]
    MRP cat
0    18   A
3    19   D
6    18   C

But if you want to access some other columns where this Boolean Series has True value, then you may use .loc:

df.loc[df['MRP']>15, 'cat']
0     A
3     D
6     C

Or, if you want to change the values where the condition is True:

df.loc[df['MRP']>15, 'cat'] = 'found'
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45