Reputation: 314
I can see that there are 2/3 ways of indexing. iloc, loc and ix (was there in earlier versions, i guess). While experimenting, I found another way that worked. Wanted clarification, why did it worked? Is loc the default method when user doesnt specify anything?
import pandas as pd
import seaborn as sns
iris = sns.load_dataset("iris")
iris[iris['species']=="setosa"].head()
and
iris.loc[iris.species == "setosa"].head()
gives the same result as
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
I checked iris type is also pandas.core.frame.DataFrame
Upvotes: 1
Views: 41
Reputation: 6483
This two give the same result becasue loc
works with either a Boolean Series or a Boolean array.
So when you give the Boolean Series (iris['species']=="setosa")
(iris['species']=="setosa")
0 True
1 True
2 True
3 True
4 True
...
145 False
146 False
147 False
148 False
149 False
Name: species, Length: 150, dtype: bool
Both (iris[iris['species']=="setosa"]
and iris.loc[iris.species == "setosa"]
) are doing a boolean indexing.
While iloc
, works only with a Boolean array, so, to use iloc
with the same mask, you can pass the Boolean Series to a numpy array:
iris.iloc[(iris['species']=="setosa").values].head()
Upvotes: 1