Indexing and Slicing In Pandas
Indexing And Slicing In Pandas
Loading Libraries :
import pandas as pdLoading Dataset :
heroes=pd.read_csv('heroes.csv')
heroes.head()sr
name
Gender
Eye color
Race
Hair color
Height
Publisher
Skin color
Alignment
Weight
Setting and Remove Indexes :
Setting an index :
sr
name
Gender
Eye color
Race
Hair color
Height
Skin color
Alignment
Weight
Resetting index to default :
index
sr
name
Gender
Eye color
Race
Hair color
Height
Publisher
Skin color
Alignment
Weight
Reset Index and drop previous index:
sr
name
Gender
Eye color
Race
Hair color
Height
Skin color
Alignment
Weight
Subsetting with .loc[]
sr
name
Gender
Eye color
Race
Hair color
Height
Publisher
Skin color
Alignment
Weight
Using .loc[]
sr
name
Gender
Eye color
Race
Hair color
Height
Skin color
Alignment
Weight
Setting multi-level indexes
Index heroes by Alignment & Publisher
sr
name
Gender
Eye color
Race
Hair color
Height
Skin color
Weight
neutral
Marvel Comics
185
Copycat
Female
red
Mutant
White
183.0
blue
67.0
Slicing index values
sr
name
Gender
Eye color
Race
Hair color
Height
Skin color
Weight
Subset rows from 'bad' to 'good'
sr
name
Gender
Eye color
Race
Hair color
Height
Skin color
Weight
Subset rows from 'good','DC Comics' to 'neutral','Marvel Comics'
Slicing time series
Subsetting by row/column number
To get the 5th row and 2nd column Value

To get First 5 Rows
sr
name
Gender
Eye color
Race
Hair color
Height
Publisher
Skin color
Alignment
Weight
To get All rows of Column 'name','Gender','Eye color':
name
Gender
Eye color
To get 10 rows of Column 'name','Gender','Eye color':
name
Gender
Eye color

Last updated