3.5. Slicing Dataframe using Loc and iLoc#

import pandas as pd
import numpy as np
df=pd.DataFrame({
'Name':['Sahil','Sonia','Sourav','Vishal'],
'Age':[10,20,30,40],
'Gender':['M','F','M','M'],
'City':['J','K','L','P'],
'Work':[True,False,False,True]
}
)
df
Name Age Gender City Work
0 Sahil 10 M J True
1 Sonia 20 F K False
2 Sourav 30 M L False
3 Vishal 40 M P True

3.5.1. Accessing columns by Names#

3.5.1.1. Accessing single column#

df['Name'] # Series
0     Sahil
1     Sonia
2    Sourav
3    Vishal
Name: Name, dtype: object
# Getting the value of column
df['Name'][2]
'Sourav'
df[['Name']] # Dataframe
Name
0 Sahil
1 Sonia
2 Sourav
3 Vishal
df.Name  # Another way
0     Sahil
1     Sonia
2    Sourav
3    Vishal
Name: Name, dtype: object

3.5.1.2. Accessing multiple columns#

df[['Name','Age','Gender']]
Name Age Gender
0 Sahil 10 M
1 Sonia 20 F
2 Sourav 30 M
3 Vishal 40 M
# Another way
list_cols=['Name','Gender']
df[list_cols]
Name Gender
0 Sahil M
1 Sonia F
2 Sourav M
3 Vishal M

3.5.1.3. Using loc#

  • df.loc[rows,columns] # Note:square brackets are used with loc

    • all rows or filtered rows

    • example, df.loc[df[‘age’]>50,’name’] === df[df[‘age’][‘name’]

    • df.loc[0,:]

    • df.loc[[1,2,3],:]

    • df.loc[[1,2,3]:[‘Name,’Age’,’Gender’]

df.loc[:,['Name','Gender']]
Name Gender
0 Sahil M
1 Sonia F
2 Sourav M
3 Vishal M

3.5.2. Accessing range of columns#

3.5.2.1. By index#

df.iloc[:,[1,4]]
Age Work
0 10 True
1 20 False
2 30 False
3 40 True

3.5.2.2. By Name#

df.loc[:,['Name','Gender']]
Name Gender
0 Sahil M
1 Sonia F
2 Sourav M
3 Vishal M
df.iloc[:,1:4]
Age Gender City
0 10 M J
1 20 F K
2 30 M L
3 40 M P
df.loc[:,'Name':'Gender']
Name Age Gender
0 Sahil 10 M
1 Sonia 20 F
2 Sourav 30 M
3 Vishal 40 M

3.5.2.3. Give all columns of 0th row#

df.loc[0,:]
Name      Sahil
Age          10
Gender        M
City          J
Work       True
Name: 0, dtype: object

3.5.2.4. Give all columns of first 3 rows#

df.loc[[0,1,2],:]
Name Age Gender City Work
0 Sahil 10 M J True
1 Sonia 20 F K False
2 Sourav 30 M L False

3.5.3. Notes#

  • loc is used to access through names

  • iloc is used to access through indexes

  • , in used to select given columns

  • : is used to select range of columns

  • if you want particular columns during fetching dataframe,use columns=list of columns you want to select

g

3.5.4. Summary#

# Get first row 
df.loc[0]
Name      Sahil
Age          10
Gender        M
City          J
Work       True
Name: 0, dtype: object
# Get first two rows 
df.loc[0:1] 
Name Age Gender City Work
0 Sahil 10 M J True
1 Sonia 20 F K False
# Get first two rows's specific column
df.loc[0:1].Name
0    Sahil
1    Sonia
Name: Name, dtype: object
# Get even numbered rows
df.loc[df.index%2==0]
Name Age Gender City Work
0 Sahil 10 M J True
2 Sourav 30 M L False
# Get odd numbered rows
df.loc[df.index%2!=0]
Name Age Gender City Work
1 Sonia 20 F K False
3 Vishal 40 M P True
# Add steps in rows
# Select every nth row
# df[df.index % n == 0]  # Selects every nth raw starting from 0
df[df.index % 2 == 0]
Name Age Gender City Work
0 Sahil 10 M J True
2 Sourav 30 M L False
# Excludes every nth row
df[df.index % 3 != 0]
Name Age Gender City Work
1 Sonia 20 F K False
2 Sourav 30 M L False