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.
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
filter_none
Output:
[[10000, '10/2/2011', 'Music'], [5000, '11/2/2011', 'Poetry'],
[15000, '12/2/2011', 'Theatre']
Using iat[] mehtod –
# 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
filter_none
Output:
[[10000, '10/2/2011', 'Music'], [5000, '11/2/2011', 'Poetry'],
[15000, '12/2/2011', 'Theatre']]
Recommended Posts:
- Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc
- Python | Pandas Dataframe.iat[ ]
- Difference between loc() and iloc() in Pandas DataFrame
- Select any row from a Dataframe in Pandas | Python
- Python | Pandas Series.iat
- Select row with maximum and minimum value in Pandas dataframe
- Python | Extracting rows using Pandas .iloc[]
- Python | Pandas Series.iloc
- Select a row of series or dataframe by given integer index
- Select first or last N rows in a Dataframe using head() and tail() method in Python-Pandas
- Apply a function to each row or column in Dataframe using pandas.apply()
- How to randomly select rows from Pandas DataFrame
- How to select multiple columns in a pandas dataframe
- How to Select Rows from Pandas DataFrame?
- Select all columns, except one given column in a Pandas DataFrame
- Python | Change column names and row indexes in Pandas DataFrame
- Apply function to every row in a Pandas DataFrame
- Add a row at top in pandas DataFrame
- Insert row at given position in Pandas Dataframe
- How to Sort a Pandas DataFrame based on column names or row index?
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.

