At times, you may need to convert a list to Pandas DataFrame in Python.
You may then use this template to convert your list to a DataFrame:
import pandas as pd list_name = ['item_1', 'item_2', 'item_3',...] df = pd.DataFrame (list_name, columns = ['column_name'])In the next section, you’ll see how to perform the conversion in practice.
Examples of Converting a List to Pandas DataFrame
Example 1: Convert a List
Let’s say that you have the following list that contains 5 products:
products_list = ['laptop', 'printer', 'tablet', 'desk', 'chair']You can then apply the following syntax in order to convert the list of products to Pandas DataFrame:
import pandas as pd products_list = ['laptop', 'printer', 'tablet', 'desk', 'chair'] df = pd.DataFrame (products_list, columns = ['product_name']) print (df)This is the DataFrame that you’ll get:
product_name 0 laptop 1 printer 2 tablet 3 desk 4 chairExample 2: Convert a List of Lists
How would you then convert a list of lists to a DataFrame?
For instance, let’s say that you have the following list of lists:
products_list = [['laptop',1300],['printer',150],['tablet',300],['desk',450],['chair',200]]You can then run the code below to perform the conversion to a DataFrame:
import pandas as pd products_list = [['laptop',1300],['printer',150],['tablet',300],['desk',450],['chair',200]] df = pd.DataFrame (products_list, columns = ['product_name', 'price']) print (df)And this is the result that you’ll get:
product_name price 0 laptop 1300 1 printer 150 2 tablet 300 3 desk 450 4 chair 200Alternatively, you may have your list of lists as follows:
products_list = [['laptop', 'printer', 'tablet', 'desk', 'chair'],[1300, 150, 300, 450, 200]]Therefore, the Python code to perform the conversion to a DataFrame would be:
import pandas as pd products_list = [['laptop', 'printer', 'tablet', 'desk', 'chair'],[1300, 150, 300, 450, 200]] df = pd.DataFrame (products_list).transpose() df.columns = ['product_name', 'price'] print (df)Run the code, and you’ll get the same DataFrame:
product_name price 0 laptop 1300 1 printer 150 2 tablet 300 3 desk 450 4 chair 200Check the Object Type
If needed, you may also check the type of the objects (e.g., List vs. DataFrame) by applying this code:
import pandas as pd products_list = [['laptop', 'printer', 'tablet', 'desk', 'chair'],[1300, 150, 300, 450, 200]] df = pd.DataFrame (products_list).transpose() df.columns = ['product_name', 'price'] print ('products_list: ' + str(type(products_list))) print ('df: ' + str(type(df)))And here is the result:
products_list: <class 'list'> df: <class 'pandas.core.frame.DataFrame'>Applying Stats Using Pandas (optional)
Once you converted your list into a DataFrame, you’ll be able to perform an assortment of operations and calculations using Pandas.
For instance, you may use Pandas to derive some statistics about your data.
In the context of our example, you can apply the code below in order to get the mean, max and min price using Pandas:
import pandas as pd products_list = [['laptop', 'printer', 'tablet', 'desk', 'chair'],[1300, 150, 300, 450, 200]] df = pd.DataFrame (products_list).transpose() df.columns = ['product_name', 'price'] mean_value = df['price'].mean() max_value = df['price'].max() min_value = df['price'].min() print ('The mean price is: ' + str(mean_value)) print ('The max price is: ' + str(max_value)) print ('The min price is: ' + str(min_value))Run the Python code, and you’ll get these stats:
The mean price is: 480 The max price is: 1300 The min price is: 150An Opposite Scenario
Sometimes, you may face an opposite situation, where you’ll need to convert a DataFrame to a list. If that’s the case, you may want to check the following guide that explains the steps to perform the conversion.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Creating Pandas Dataframe can be achieved in multiple ways. Let’s see how can we create a Pandas DataFrame from Lists.
Code #1: Basic example
import pandas as pd
lst = ['Geeks', 'For', 'Geeks', 'is',
'portal', 'for', 'Geeks']
df = pd.DataFrame(lst)
df
Output:
Code #2: Dataframe using list with index and column names
import pandas as pd
lst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']
df = pd.DataFrame(lst, index =['a', 'b', 'c', 'd', 'e', 'f', 'g'],
columns =['Names'])
df
Output:
Code #3: Using zip() for zipping two lists
import pandas as pd
lst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']
lst2 = [11, 22, 33, 44, 55, 66, 77]
df = pd.DataFrame(list(zip(lst, lst2)),
columns =['Name', 'val'])
df
Output:
Code #4: Creating DataFrame using multi-dimensional list
import pandas as pd
lst = [['tom', 25], ['krish', 30],
['nick', 26], ['juli', 22]]
df = pd.DataFrame(lst, columns =['Name', 'Age'])
df
Output:
Code #5: Using multi-dimensional list with column name and dtype specified.
import pandas as pd
lst = [['tom', 'reacher', 25], ['krish', 'pete', 30],
['nick', 'wilson', 26], ['juli', 'williams', 22]]
df = pd.DataFrame(lst, columns =['FName', 'LName', 'Age'], dtype = float)
df
Output:
Code #6: Using lists in dictionary to create dataframe
import pandas as pd
nme = ["aparna", "pankaj", "sudhir", "Geeku"]
deg = ["MBA", "BCA", "M.Tech", "MBA"]
scr = [90, 40, 80, 98]
dict = {'name': nme, 'degree': deg, 'score': scr}
df = pd.DataFrame(dict)
df
Output: