Select rows and columns by name or index in Pandas

Pandas

In this post, we are going to understand how to select rows and columns by name or index in Pandas by using the built-in function.

Ways to Select rows and columns by name or index


  • Using operator [] to select value by column name.
  • Using Dataframe.loc[] label based
  • Using Dataframe.iloc[] postion based
  • Using at[] and iat[] to select a scalar value.

1. Using [] operator select column by name


We pass the column name to the [] operator to access the data.

1.1 Select single column by name

import pandas as pd
Stu_Data = [('Jack',100,'Math'),
 ( 'Rack',100,'Math'),
('Max',100,'Music'),
('David', 100,'Math')
]

df_stu = pd.DataFrame(Stu_Data, columns = ['Name' , 'Marks', 'Subject'], index=['R1', 'R2', 'R3','R4'])

print('\n selected column:\n',df_stu["Name"])

Output

 selected column:
 R1     Jack
R2     Rack
R3      Max
R4    David
Name: Name, dtype: object

1.2 Select multiple column by name


import pandas as pd
Stu_Data = [('Jack',100,'Math'),
 ( 'Rack',100,'Math'),
('Max',100,'Music'),
('David', 100,'Math')
]

df_stu = pd.DataFrame(Stu_Data, columns = ['Name' , 'Marks', 'Subject'], index=['R1', 'R2', 'R3','R4'])

print('\n selected column:\n',df_stu[['Name' , 'Marks', 'Subject']])

Output

 selected column:
      Name  Marks Subject
R1   Jack    100    Math
R2   Rack    100    Math
R3    Max    100   Music
R4  David    100    Math

2. Dataframe.loc[] to select rows and column by name


The data Dataframe. loc[] is a label based we have to pass the row and column that we need to filter.

2.1 loc[] to Select single column and all rows

The colon(:) is used to accessing all rows as we are accessing in the below example.

import pandas as pd
Stu_Data = [('Jack',100,'Math'),
 ( 'Rack',100,'Math'),
('Max',100,'Music')
]

df_stu = pd.DataFrame(Stu_Data, columns = ['Name' , 'Marks', 'Subject'], index=['R1', 'R2', 'R3'])

print(df_stu)

print('\n selected single columns:\n',df_stu.loc[ : , 'Name' ])

print('\n select mutiple columns:\n',df_stu.loc[ : , ['Name', 'Marks']])

Output

    Name  Marks Subject
R1  Jack    100    Math
R2  Rack    100    Math
R3   Max    100   Music

 selected single columns:
 R1    Jack
R2    Rack
R3     Max
Name: Name, dtype: object


2.2 Select multiple columns and all rows

import pandas as pd
Stu_Data = [('Jack',100,'Math'),
 ( 'Rack',100,'Math'),
('Max',100,'Music')
]

df_stu = pd.DataFrame(Stu_Data, columns = ['Name' , 'Marks', 'Subject'], index=['R1', 'R2', 'R3'])


print('\n select mutiple columns:\n',df_stu.loc[ : , ['Name', 'Marks']])

Output

 select mutiple columns:
     Name  Marks
R1  Jack    100
R2  Rack    100
R3   Max    100

2.3 loc[] to Select single row and all columns


import pandas as pd
Stu_Data = [('Jack',100,'Math'),
 ( 'Rack',100,'Math'),
('Max',100,'Music')
]

df_stu = pd.DataFrame(Stu_Data, columns = ['Name' , 'Marks', 'Subject'], index=['R1', 'R2', 'R3'])



print('\n selected single row:\n',df_stu.loc[ 'R2' , : ])

Output

 selected single row:
 Name       Rack
Marks       100
Subject    Math
Name: R2, dtype: object

2.4 loc[] to Select mutiple row and all columns


import pandas as pd
Stu_Data = [('Jack',100,'Math'),
 ( 'Rack',100,'Math'),
('Max',100,'Music')
]

df_stu = pd.DataFrame(Stu_Data, columns = ['Name' , 'Marks', 'Subject'], index=['R1', 'R2', 'R3'])

print('\n select mutiple rows:\n',df_stu.loc[ ['R1' , 'R2'] , :])

Output

 select mutiple rows:
     Name  Marks Subject
R1  Jack    100    Math
R2  Rack    100    Math

2.5 Select mutiple rows and columns simultaneously


import pandas as pd
Stu_Data = [('Jack',100,'Math'),
 ( 'Rack',100,'Math'),
('Max',100,'Music')
]

df_stu = pd.DataFrame(Stu_Data, columns = ['Name' , 'Marks', 'Subject'], index=['R1', 'R2', 'R3'])


print('\n select mutiple rows and columns:\n',df_stu.loc[ ['R1' , 'R2'] , ['Name' , 'Marks']])

Output

 select mutiple rows and columns:
     Name  Marks
R1  Jack    100
R2  Rack    100

3. Dataframe.iloc[] to Select column and row by index


It is integer index-based which means we have to specify rows and columns by integer index.

3.1 iloc[] to Select single column by index postion

import pandas as pd
Stu_Data = [('Jack',100,'Math'),
 ( 'Rack',100,'Math'),
('Max',100,'Music'),
('David', 100,'Math')
]

df_stu = pd.DataFrame(Stu_Data, columns = ['Name' , 'Marks', 'Subject'], index=['R1', 'R2', 'R3','R4'])


print('\n selected column:\n',df_stu.iloc[ : , 0 ])

Output

 selected column:
 R1     Jack
R2     Rack
R3      Max
R4    David
Name: Name, dtype: object

3.2 Select multiple column by indexed in list


import pandas as pd
Stu_Data = [('Jack',100,'Math'),
 ( 'Rack',100,'Math'),
('Max',100,'Music'),
('David', 100,'Math')
]

df_stu = pd.DataFrame(Stu_Data, columns = ['Name' , 'Marks', 'Subject'], index=['R1', 'R2', 'R3','R4'])

print('\n selected columns:\n',df_stu.iloc[ : , [0,1]])

Output

 selected columns:
      Name  Marks
R1   Jack    100
R2   Rack    100
R3    Max    100
R4  David    100

3.3 Select a single row by index Position


import pandas as pd
Stu_Data = [('Jack',100,'Math'),
 ( 'Rack',100,'Math'),
('Max',100,'Music')
]

df_stu = pd.DataFrame(Stu_Data, columns = ['Name' , 'Marks', 'Subject'], index=['R1', 'R2', 'R3'])



print('\n selected rows:\n',df_stu.iloc[ 2 ,:])



Output

 selected rows:
 Name         Max
Marks        100
Subject    Music
Name: R3, dtype: object

3.4 Select mutlple rows by indexes in a list


import pandas as pd
Stu_Data = [('Jack',100,'Math'),
 ( 'Rack',100,'Math'),
('Max',100,'Music')
]

df_stu = pd.DataFrame(Stu_Data, columns = ['Name' , 'Marks', 'Subject'], index=['R1', 'R2', 'R3'])

print('\n selected rows:\n',df_stu.iloc[ 0:2 ,:])

Output

 selected rows:
     Name  Marks Subject
R1  Jack    100    Math
R2  Rack    100    Math

3.5 Select mutiple row and column by index position


import pandas as pd
Stu_Data = [('Jack',100,'Math'),
 ( 'Rack',100,'Math'),
('Max',100,'Music')
]

df_stu = pd.DataFrame(Stu_Data, columns = ['Name' , 'Marks', 'Subject'], index=['R1', 'R2', 'R3'])

print('\n selected columns:\n',df_stu.iloc[ 1 : 2 ,  0 : 2 ])

Output

 selected columns:
     Name  Marks
R2  Rack    100

4. DataFrame.at[] and .iat[] to Select scalar value


  • DataFrame.at[] : access the value of row and column by label.
  • DataFrame.iat[] : Access the value row and column by index.

Program Example

import pandas as pd
Stu_Data = [('Jack',100,'Math'),
 ( 'Rack',100,'Math'),
('Max',100,'Music'),
('David', 100,'Math')
]

df_stu = pd.DataFrame(Stu_Data, columns = ['Name' , 'Marks', 'Subject'], index=['R1', 'R2', 'R3','R4'])



print('selecetd values :',df_stu.at['R1', "Name"])

print('selecetd values :',df_stu.at['R1', "Name"])

Output

selecetd values : Jack

Summary

in this post, we have learned multiple ways to Select rows and columns by name or index in Pandas.