Reputation: 685
Does anyone know the fundamental difference between the .filter method and the .loc method in Pandas? They seem to do the same thing. Thanks.
Upvotes: 10
Views: 6466
Reputation: 2293
.loc[]
is a Purely label-location based indexer for selection by label. It fails when the selection isn't found, only accepts certain types of input and works on only one axis of your dataframe.
df.filter()
returns Subset rows or columns of dataframe according to labels in the specified index. You can filter along either axis, and you can filter in more advanced ways than with loc
.
filter
will return the same type of object as the caller, whereas loc
will return the value specified by the label (so a Series if caller is a DF, a scalar if caller is a Series).
In short, .loc
is for accessing a specific item within the caller, .filter()
is for applying a filter to the caller and returning only items which match that filter.
Upvotes: 7