Select any row from a Dataframe using iloc[] and iat[] in Pandas

In this article, we will learn how to get the rows from a dataframe as a list, using the functions ilic[] and iat[]. There are multiple ways to do get the rows as a list from given dataframe. Let’s see them will the help of examples.

filter_none

edit
close

play_arrow

link
brightness_4
code

import pandas as pd 
    
# Create the dataframe 
df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/11'], 
                    'Event':['Music', 'Poetry', 'Theatre', 'Comedy'], 
                    'Cost':[10000, 5000, 15000, 2000]}) 
  
# Create an empty list 
Row_list =[] 
    
# Iterate over each row 
for i in range((df.shape[0])): 
    
    # Using iloc to access the values of  
    # the current row denoted by "i" 
    Row_list.append(list(df.iloc[i, :])) 
    
# Print the first 3 elements 
print(Row_list[:3]) 

chevron_right


Output:

[[10000, '10/2/2011', 'Music'], [5000, '11/2/2011', 'Poetry'],
      [15000, '12/2/2011', 'Theatre']

 
Using iat[] mehtod –

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd 
import pandas as pd 
    
# Create the dataframe 
df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/11'], 
                    'Event':['Music', 'Poetry', 'Theatre', 'Comedy'], 
                    'Cost':[10000, 5000, 15000, 2000]}) 
    
# Create an empty list 
Row_list =[] 
    
# Iterate over each row 
for i in range((df.shape[0])): 
    # Create a list to store the data 
    # of the current row 
    cur_row =[] 
        
    # iterate over all the columns 
    for j in range(df.shape[1]): 
            
        # append the data of each 
        # column to the list 
        cur_row.append(df.iat[i, j]) 
            
    # append the current row to the list 
    Row_list.append(cur_row) 
  
# Print the first 3 elements 
print(Row_list[:3])

chevron_right


Output:

[[10000, '10/2/2011', 'Music'], [5000, '11/2/2011', 'Poetry'], 
      [15000, '12/2/2011', 'Theatre']]



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.