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.
Python
import pandas as pd
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 ]})
Row_list = []
for i in range ((df.shape[ 0 ])):
Row_list.append( list (df.iloc[i, :]))
print (Row_list[: 3 ])
|
Output:
[[10000, '10/2/2011', 'Music'], [5000, '11/2/2011', 'Poetry'],
[15000, '12/2/2011', 'Theatre']
Using iat[] method –
Python3
import pandas as pd
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 ]})
Row_list = []
for i in range ((df.shape[ 0 ])):
cur_row = []
for j in range (df.shape[ 1 ]):
cur_row.append(df.iat[i, j])
Row_list.append(cur_row)
print (Row_list[: 3 ])
|
Output:
[[10000, '10/2/2011', 'Music'], [5000, '11/2/2011', 'Poetry'],
[15000, '12/2/2011', 'Theatre']]
Last Updated :
23 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...