Convert a List to Dataframe in Python (3 examples)

At times, you may need to convert your list to a DataFrame in Python.

You may then use this template to convert your list to pandas DataFrame:

import pandas as pd
your_list = ['item1', 'item2', 'item3',...]
df = pd.DataFrame.from_dict (your_list)

In the next section, I’ll review 3 examples to show you how to perform the conversion in practice.

Examples of Converting a List to DataFrame in Python

Example 1: Convert a List

Let’s say that you have the following list that contains the names of 5 people:

People_List = ['Jon', 'Mark', 'Maria','Jill','Jack']

You can then apply the from_dict syntax in order to convert the list of names to pandas DataFrame:

import pandas as pd

People_List = ['Jon', 'Mark', 'Maria','Jill','Jack']

df = pd.DataFrame.from_dict (People_List)
print (df)

This is the DataFrame that you’ll get:

Convert a List to Dataframe in Python

Example 2: Convert List of Lists

How would you then convert a list of lists to a DataFrame?

The same principles apply whether you’re using a list or a list of lists.

For instance, let’s say that you have the following list of lists:

People_List = [['Jon', 'Mark', 'Maria','Jill','Jack'],['Smith', 'Brown', 'Lee', 'Jones', 'Ford'],[21, 38, 42, 28, 55]]

You can then run the code below to convert your list of lists:

import pandas as pd

People_List = [['Jon', 'Mark', 'Maria','Jill','Jack'],['Smith', 'Brown', 'Lee', 'Jones', 'Ford'],[21, 38, 42, 28, 55]]

df = pd.DataFrame.from_dict (People_List)
print (df)

And this is the result that you’ll get:

Convert List of lists to Dataframe

Example 3: Column Orientation

You can also structure the data such that you’ll get the following column orientation:

First_NameLast_NameAge
JonSmith21
MarkBrown38
MariaLee42
JillJones28
JackFord55

You can capture the above table in Python as follows:

People_List = [('First_Name', ['Jon', 'Mark', 'Maria','Jill','Jack']),
               ('Last_Name', ['Smith', 'Brown', 'Lee', 'Jones', 'Ford']),
               ('Age', [21, 38, 42, 28, 55])]

Unlike the previous 2 examples, you’ll need to use the following code structure to perform the conversion to the DataFrame:

df = pd.DataFrame.from_dict (dict(People_List))

Notice that there is an additional ‘dict’ within the brackets.

Putting everything together:

import pandas as pd

People_List = [('First_Name', ['Jon', 'Mark', 'Maria','Jill','Jack']),
               ('Last_Name', ['Smith', 'Brown', 'Lee', 'Jones', 'Ford']),
               ('Age', [21, 38, 42, 28, 55])]

df = pd.DataFrame.from_dict (dict(People_List))
print (df)

Once you run the code, you’ll get this DataFrame:

Convert a List to Dataframe in Python (3 examples)

If needed, you can also check the type of the objects (e.g., List vs. DataFrame) by applying this code:

import pandas as pd

People_List = [('First_Name', ['Jon', 'Mark', 'Maria','Jill','Jack']),
               ('Last_Name', ['Smith', 'Brown', 'Lee', 'Jones', 'Ford']),
               ('Age', [21, 38, 42, 28, 55])]

df = pd.DataFrame.from_dict (dict(People_List))

print ('People_List: ' + str(type(People_List)))
print ('df: ' + str(type(df)))

And here is the result:

Type of 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 can 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 age using pandas:

import pandas as pd

People_List = [('First_Name', ['Jon', 'Mark', 'Maria','Jill','Jack']),
               ('Last_Name', ['Smith', 'Brown', 'Lee', 'Jones', 'Ford']),
               ('Age', [21, 38, 42, 28, 55])]

df = pd.DataFrame.from_dict (dict(People_List))

mean1 = df['Age'].mean()
max1 = df['Age'].max()
min1 = df['Age'].min()

print ('The mean age is: ' + str(mean1))
print ('The max age is: ' + str(max1))
print ('The min age is: ' + str(min1))

#print (df)

Run the Python code, and you’ll get these stats:

Stats pandas

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 source that explains how to convert a DataFrame to a list.