How do i convert a list into a dataframe in python?

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        chair

Example 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    200

Alternatively, 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    200

Check 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: 150

An 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

  • Read
  • Discuss
  • 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:

    How do i convert a list into a dataframe in python?

     
    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:

    How do i convert a list into a dataframe in python?

     

    How do i convert a list into a dataframe in python?

    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:

    How do i convert a list into a dataframe in python?

     
    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:

    How do i convert a list into a dataframe in python?

     
    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:

    How do i convert a list into a dataframe in python?

     
    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:

    How do i convert a list into a dataframe in python?


    Can we convert list to DataFrame?

    Just like list of lists we can pass list of tuples in dataframe constructor to create a dataframe. Suppose we have a list of tuples i.e. Pass this list of tuples to DataFrame's constructor to create a DataFrame object i.e. Both Column & Index labels are default.

    How do I convert a list into a DataFrame column?

    Algorithm.
    Create DataFrame using a dictionary..
    Create a list containing new column data. Make sure that the length of the list matches the length of the data which is already present in the data frame..
    Insert the data into the DataFrame using DataFrame. assign(column_name = data) method. It returns a new data frame..

    How do I convert a list to pandas?

    Convert data to list. Since there is no method to convert pandas. DataFrame , pandas. Series directly to list , first get the NumPy array ndarray with the values attribute, and then use tolist() method to convert to list .

    How do I turn a list into a table in Python?

    How to Easily Create Tables in Python.
    install tabulate. We first install the tabulate library using pip install in the command line: pip install tabulate..
    import tabulate function. ... .
    list of lists. ... .
    dictionary of iterables. ... .
    missing values..