2.Indexing and Slicing In Pandas
Indexing And Slicing In Pandas
Loading Libraries :
In [1]:
Loading Dataset :
In [8]:
0
0
A-Bomb
Male
yellow
Human
No Hair
203.0
Marvel Comics
-
good
441.0
1
1
Abe Sapien
Male
blue
Icthyo Sapien
No Hair
191.0
Dark Horse Comics
blue
good
65.0
2
2
Abin Sur
Male
blue
Ungaran
No Hair
185.0
DC Comics
red
good
90.0
3
3
Abomination
Male
green
Human / Radiation
No Hair
203.0
Marvel Comics
-
bad
441.0
4
4
Abraxas
Male
blue
Cosmic Entity
Black
-99.0
Marvel Comics
-
bad
-99.0
We will perform indexing and slicing operation on this Dataset.
First we will have to learn how to set and remove indexes.
Setting and Remove Indexes :
Setting an index :
pandas allows us to set column as an index. Say , if we want to set 'Publisher' as an index.
In [18]:
Publisher
Marvel Comics
0
A-Bomb
Male
yellow
Human
No Hair
203.0
-
good
441.0
Dark Horse Comics
1
Abe Sapien
Male
blue
Icthyo Sapien
No Hair
191.0
blue
good
65.0
DC Comics
2
Abin Sur
Male
blue
Ungaran
No Hair
185.0
red
good
90.0
Marvel Comics
3
Abomination
Male
green
Human / Radiation
No Hair
203.0
-
bad
441.0
Marvel Comics
4
Abraxas
Male
blue
Cosmic Entity
Black
-99.0
-
bad
-99.0
Resetting index to default :
In [19]:
0
0
0
A-Bomb
Male
yellow
Human
No Hair
203.0
Marvel Comics
-
good
441.0
1
1
1
Abe Sapien
Male
blue
Icthyo Sapien
No Hair
191.0
Dark Horse Comics
blue
good
65.0
2
2
2
Abin Sur
Male
blue
Ungaran
No Hair
185.0
DC Comics
red
good
90.0
3
3
3
Abomination
Male
green
Human / Radiation
No Hair
203.0
Marvel Comics
-
bad
441.0
4
4
4
Abraxas
Male
blue
Cosmic Entity
Black
-99.0
Marvel Comics
-
bad
-99.0
...
...
...
...
...
...
...
...
...
...
...
...
...
729
729
729
Yellowjacket II
Female
blue
Human
Strawberry Blond
165.0
Marvel Comics
-
good
52.0
730
730
730
Ymir
Male
white
Frost Giant
No Hair
304.8
Marvel Comics
white
good
-99.0
731
731
731
Yoda
Male
brown
Yoda's species
White
66.0
George Lucas
green
good
17.0
732
732
732
Zatanna
Female
blue
Human
Black
170.0
DC Comics
-
good
57.0
733
733
733
Zoom
Male
red
-
Brown
185.0
DC Comics
-
bad
81.0
Reset Index and drop previous index:
In [22]:
0
0
A-Bomb
Male
yellow
Human
No Hair
203.0
-
good
441.0
1
1
Abe Sapien
Male
blue
Icthyo Sapien
No Hair
191.0
blue
good
65.0
2
2
Abin Sur
Male
blue
Ungaran
No Hair
185.0
red
good
90.0
3
3
Abomination
Male
green
Human / Radiation
No Hair
203.0
-
bad
441.0
4
4
Abraxas
Male
blue
Cosmic Entity
Black
-99.0
-
bad
-99.0
...
...
...
...
...
...
...
...
...
...
...
729
729
Yellowjacket II
Female
blue
Human
Strawberry Blond
165.0
-
good
52.0
730
730
Ymir
Male
white
Frost Giant
No Hair
304.8
white
good
-99.0
731
731
Yoda
Male
brown
Yoda's species
White
66.0
green
good
17.0
732
732
Zatanna
Female
blue
Human
Black
170.0
-
good
57.0
733
733
Zoom
Male
red
-
Brown
185.0
-
bad
81.0
Subsetting with .loc[]
We use .loc[] over the standard square bracket subsetting because .loc[] is convenient, easy to read & understand by other developer. Now, we will see the difference between both the subsetting then you will be able to understand how .loc[] works.
In [24]:
0
0
A-Bomb
Male
yellow
Human
No Hair
203.0
Marvel Comics
-
good
441.0
2
2
Abin Sur
Male
blue
Ungaran
No Hair
185.0
DC Comics
red
good
90.0
3
3
Abomination
Male
green
Human / Radiation
No Hair
203.0
Marvel Comics
-
bad
441.0
4
4
Abraxas
Male
blue
Cosmic Entity
Black
-99.0
Marvel Comics
-
bad
-99.0
5
5
Absorbing Man
Male
blue
Human
No Hair
193.0
Marvel Comics
-
bad
122.0
...
...
...
...
...
...
...
...
...
...
...
...
728
728
Yellowjacket
Male
blue
Human
Blond
183.0
Marvel Comics
-
good
83.0
729
729
Yellowjacket II
Female
blue
Human
Strawberry Blond
165.0
Marvel Comics
-
good
52.0
730
730
Ymir
Male
white
Frost Giant
No Hair
304.8
Marvel Comics
white
good
-99.0
732
732
Zatanna
Female
blue
Human
Black
170.0
DC Comics
-
good
57.0
733
733
Zoom
Male
red
-
Brown
185.0
DC Comics
-
bad
81.0
Using .loc[]
In [28]:
Publisher
Marvel Comics
0
A-Bomb
Male
yellow
Human
No Hair
203.0
-
good
441.0
Marvel Comics
3
Abomination
Male
green
Human / Radiation
No Hair
203.0
-
bad
441.0
Marvel Comics
4
Abraxas
Male
blue
Cosmic Entity
Black
-99.0
-
bad
-99.0
Marvel Comics
5
Absorbing Man
Male
blue
Human
No Hair
193.0
-
bad
122.0
Marvel Comics
8
Agent 13
Female
blue
-
Blond
173.0
-
good
61.0
...
...
...
...
...
...
...
...
...
...
...
DC Comics
715
Wildfire
Male
-
-
-
-99.0
-
good
-99.0
DC Comics
720
Wonder Girl
Female
blue
Demi-God
Blond
165.0
-
good
51.0
DC Comics
722
Wonder Woman
Female
blue
Amazon
Black
183.0
-
good
74.0
DC Comics
732
Zatanna
Female
blue
Human
Black
170.0
-
good
57.0
DC Comics
733
Zoom
Male
red
-
Brown
185.0
-
bad
81.0
Setting multi-level indexes
Indexes can also be made out of multiple columns, forming a multi-level index (sometimes called a hierarchical index). The benefit is that multi-level indexes make it more natural to reason about nested categorical variables. Let's make multi-index in our data and subset it.
Index heroes by Alignment & Publisher
In [29]:
Alignment
Publisher
good
Marvel Comics
0
A-Bomb
Male
yellow
Human
No Hair
203.0
-
441.0
Dark Horse Comics
1
Abe Sapien
Male
blue
Icthyo Sapien
No Hair
191.0
blue
65.0
DC Comics
2
Abin Sur
Male
blue
Ungaran
No Hair
185.0
red
90.0
bad
Marvel Comics
3
Abomination
Male
green
Human / Radiation
No Hair
203.0
-
441.0
Marvel Comics
4
Abraxas
Male
blue
Cosmic Entity
Black
-99.0
-
-99.0
Slicing index values
Slicing lets you select consecutive elements of an object using first:last syntax. DataFrames can be sliced by index values or by row/column number, we will start with the first case. This involves slicing inside the .loc[] method.
In [33]:
Alignment
Publisher
-
DC Comics
676
Trickster
Male
blue
Human
Blond
183.0
-
81.0
Image Comics
426
Man of Miracles
-
blue
God / Eternal
Silver
-99.0
-
-99.0
Marvel Comics
33
Anti-Venom
Male
blue
Symbiote
Blond
229.0
-
358.0
Marvel Comics
110
Blackwulf
Male
red
Alien
White
188.0
-
88.0
Marvel Comics
692
Venompool
Male
-
Symbiote
-
226.0
-
-99.0
Subset rows from 'bad' to 'good'
In [34]:
Alignment
Publisher
bad
DC Comics
19
Amazo
Male
red
Android
-
257.0
-
173.0
DC Comics
31
Anti-Monitor
Male
yellow
God / Eternal
No Hair
61.0
-
-99.0
DC Comics
48
Atlas
Male
blue
God / Eternal
Brown
198.0
-
126.0
DC Comics
59
Bane
Male
-
Human
-
203.0
-
180.0
DC Comics
80
Big Barda
Female
blue
New God
Black
188.0
-
135.0
...
...
...
...
...
...
...
...
...
...
...
good
NaN
381
Katniss Everdeen
Female
-
Human
-
-99.0
-
-99.0
NaN
389
King Kong
Male
yellow
Animal
Black
30.5
-
NaN
NaN
393
Kool-Aid Man
Male
black
-
No Hair
-99.0
red
-99.0
NaN
542
Rambo
Male
brown
Human
Black
178.0
-
83.0
NaN
658
The Cape
Male
-
-
-
-99.0
-
-99.0
Subset rows from 'good','DC Comics' to 'neutral','Marvel Comics'
Slicing series
Subsetting by row/column number
It is useful to subset rows with their row numbers. This is done with .iloc[], and like .loc[] it can take two arguments to let you subset by rows and columns.
To get the 5th row and 2nd column Value
In [42]:
Out[42]:
To get First 5 Rows
In [43]:
0
0
A-Bomb
Male
yellow
Human
No Hair
203.0
Marvel Comics
-
good
441.0
1
1
Abe Sapien
Male
blue
Icthyo Sapien
No Hair
191.0
Dark Horse Comics
blue
good
65.0
2
2
Abin Sur
Male
blue
Ungaran
No Hair
185.0
DC Comics
red
good
90.0
3
3
Abomination
Male
green
Human / Radiation
No Hair
203.0
Marvel Comics
-
bad
441.0
4
4
Abraxas
Male
blue
Cosmic Entity
Black
-99.0
Marvel Comics
-
bad
-99.0
To get All rows of Column 'name','Gender','Eye color':
In [44]:
0
A-Bomb
Male
yellow
1
Abe Sapien
Male
blue
2
Abin Sur
Male
blue
3
Abomination
Male
green
4
Abraxas
Male
blue
...
...
...
...
729
Yellowjacket II
Female
blue
730
Ymir
Male
white
731
Yoda
Male
brown
732
Zatanna
Female
blue
733
Zoom
Male
red
To get 10 rows of Column 'name','Gender','Eye color':
In [45]:
0
A-Bomb
Male
yellow
1
Abe Sapien
Male
blue
2
Abin Sur
Male
blue
3
Abomination
Male
green
4
Abraxas
Male
blue
Last updated