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']]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Aug, 2021
Like Article
Save Article