Convert List to Dataframe Python
In this post, you will learn how to convert a single or multiple lists to a DataFrame.
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 used widely. Lists play an effective role in small projects as it consumes more memory.
A Dataframe is a tabular-like data structure containing an ordered collection of columns. The 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.
The Python pandas library provides a constructor of DataFrame to create a Dataframe by passing objects.
pandas.DataFrame( data, index, columns, dtype, copy)
Here, the data can be ndarray, series, dict, map, lists, constants and also dataframe, the index is used for the resulting frame, the columns are used for column labels, dtype is the data type of each column and the copy is used for copying data.
Example 1: Convert a List to Dataframe
Suppose we have the following list that contains the names of 4 flowers:
data = ["Rose","Tulip","Sunflower","Lilly"]
Here, we have converted the list of names to pandas DataFrame:
import pandas as pd
data = ["Rose","Tulip","Sunflower","Lilly"]
getData = pd.DataFrame(data,columns=['FLOWERS'])
print(getData)
Output of the above code:
FLOWERS
0 Rose
1 Tulip
2 Sunflower
3 Lilly
Example 2: Convert List of Lists to Dataframe
Suppose we have the following list of lists:
fruit_chart = [['Apple',12,200],['Banana',20,200],['Pear',20,140],['Kiwi',31,300],['Pineapple',10,550]]
The following code below to perform the conversion to the DataFrame:
from pandas import DataFrame
fruit_chart = [['Apple',12,200],['Banana',20,200],
['Pear',20,140],['Kiwi',31,300],['Pineapple',10,550]]
df = DataFrame(fruit_chart,columns=['Fruit','Quantity','Price'])
print(df)
Output of the above code:
Fruit Quantity Price
0 Apple 12 200
1 Banana 20 200
2 Pear 20 140
3 Kiwi 31 300
4 Pineapple 10 550
We can flip the representation of Dataframe using transpose() method.
from pandas import DataFrame
fruit_chart = [['Apple',12,200],['Banana',20,200],
['Pear',20,140],['Kiwi',31,300],['Pineapple',10,550]]
df = DataFrame(fruit_chart,columns=['Fruit','Quantity','Price']).transpose()
print(df)
Output of the above code:
0 1 2 3 4
Fruit Apple Banana Pear Kiwi Pineapple
Quantity 12 20 20 31 10
Price 200 200 140 300 550
Related Articles
Convert Python list to numpy array
Convert 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
Write Python Pandas Dataframe to CSV
Python Pandas DataFrame
Python Pandas CSV to Dataframe
Python Pandas Plotting
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python take screenshot of specific window
Read data from excel file using Python Pandas
Quick Introduction to Python Pandas
Python requests GET method
Python Convert XML to CSV
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword