Create a list from rows in Pandas DataFrame | Set 2
In an earlier post, we had discussed some approaches to extract the rows of the dataframe as a Python’s list. In this post, we will see some more methods to achieve that goal.
Note : For link to the CSV file used in the code, click here.
Solution #1: In order to access the data of each row of the Pandas dataframe, we can use
DataFrame.iloc attribute and then we can append the data of each row to the end of the list.
Now we will use the
DataFrame.iloc attribute to access the values of each row in the dataframe and then we will construct a list out of it.
As we can see in the output, we have successfully extracted each row of the given dataframe into a list. Just like any other Python’s list we can perform any list operation on the extracted list.
Solution #2: In order to access the data of each row of the Pandas dataframe we can use
DataFrame.iat attribute and then we can append the data of each row to the end of the list.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course