Limited rows selection with given column in Pandas | Python

Methods in Pandas like iloc[], iat[] are generally used to select the data from a given dataframe. In this article, we will learn how to select the limited rows with given columns with the help of these methods.

Example 1: Select two columns

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# Define a dictionary containing employee data 
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'], 
        'Age':[27, 24, 22, 32], 
        'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'], 
        'Qualification':['Msc', 'MA', 'MCA', 'Phd']} 
    
# Convert the dictionary into DataFrame  
df = pd.DataFrame(data) 
    
# select three rows and two columns 
print(df.loc[1:3, ['Name', 'Qualification']])

chevron_right


Output:

     Name Qualification
1  Princi            MA
2  Gaurav           MCA
3    Anuj           Phd

Example 2: First filtering rows and selecting columns by label format and then Select all columns.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# Define a dictionary containing employee data 
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'], 
        'Age':[27, 24, 22, 32], 
        'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'], 
        'Qualification':['Msc', 'MA', 'MCA', 'Phd'
       
  
# Convert the dictionary into DataFrame  
df = pd.DataFrame(data) 
    
# .loc DataFrame method 
# filtering rows and selecting columns by label format 
# df.loc[rows, columns] 
# row 1, all columns 
print(df.loc[0, :] )

chevron_right


Output:

Address          Delhi
Age                 27
Name               Jai
Qualification      Msc
Name: 0, dtype: object

Example 3: Select all or some columns, one to another using .iloc.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# Define a dictionary containing employee data 
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'], 
        'Age':[27, 24, 22, 32], 
        'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'], 
        'Qualification':['Msc', 'MA', 'MCA', 'Phd']} 
    
# Convert the dictionary into DataFrame  
df = pd.DataFrame(data) 
    
# iloc[row slicing, column slicing] 
print(df.iloc [0:2, 1:3] )

chevron_right


Output:

   Age    Name
0   27     Jai
1   24  Princi



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.