Convert dataframe to list Python
In this post, you will learn how to convert a dataframe to a list in Python.
A DataFrame is a tabular-like data structure containing an ordered collection of columns. Each column can be of different data types, like numeric, boolean, strings, etc. It has both a row and a column index. It is basically used for analytical purposes.
A list is an arranged collection of elements. It is used to store collections of data. It can contain a list of various types of data objects with a comma separated and enclosed within a square bracket. It is mutable, which means we can change the order of elements. Individual elements can be replaced even after the list has been created. This is one of the leading reasons why lists are being widely used. Lists play an effective role in small projects as they consume more memory. Here, we have mentioned different ways to convert a dataframe to a list.
Convert dataframe to list using tolist() method
Python provides tolist() method to convert a dataframe to list. The syntax is as follows-
df.values.tolist()
Here is the simple code to convert a dataframe to list.
import pandas as pd
# dictionary of lists
stu_dict = {'Name': ["Smith", "Priska", "Kashyap", "Andy"]
,'Subject': ["Phy", "Maths", "Chm", "Bio"],
'Marks': [90, 99, 92, 89]}
df = pd.DataFrame(stu_dict)
stu_list = df.values.tolist()
print(stu_list)
Output of the above code:
[['Smith', 'Phy', 90], ['Priska', 'Maths', 99], ['Kashyap', 'Chm', 92], ['Andy', 'Bio', 89]]
In the output, we get a multi-dimensional list. We can also convert an individual column in the dataframe into a list. The following example demonstrates this:
import pandas as pd
# dictionary of lists
stu_dict = {'Name': ["Smith", "Priska", "Kashyap", "Andy"]
,'Subject': ["Phy", "Maths", "Chm", "Bio"],
'Marks': [90, 99, 92, 89]}
df = pd.DataFrame(stu_dict)
stu_list = df['Name'].values.tolist()
print(stu_list)
Output of the above code:
['Smith', 'Priska', 'Kashyap', 'Andy']
Convert dataframe to list using zip() function
Python's zip() function creates an iterator that will aggregate elements from at least two iterables. It returns a zip object, which is an iterator of lists where the main thing in each passed iterator is matched together, and afterward, the second thing in each passed iterator is combined together, and so on. If Python zip work gets no iterable components, it returns an empty iterator. In the given example, we have used this to convert a dataframe to a list.
import pandas as pd
# Creating Dictionary
data = [['Biscuit',6],['Chips',3],['Candy',20]]
getData = pd.DataFrame(data,columns=['Item','Quantity'],
index=[101,102,103])
listOfVal = [list(i) for i in zip(*getData.values)]
print(listOfVal)
Output of the above code:
[['Biscuit', 'Chips', 'Candy'], [6, 3, 20]]
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary