Convert a List to Dataframe in Python (with 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:

from pandas import DataFrame
your_list = ['item1', 'item2', 'item3',...]
df = DataFrame (your_list,columns=['Column_Name'])

In the next section, I’ll review few 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 following syntax in order to convert the list of names to pandas DataFrame:

from pandas import DataFrame

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

df = DataFrame (People_List,columns=['First_Name'])
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?

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

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

You can then run the code below to perform the conversion to the DataFrame:

from pandas import DataFrame

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

df = DataFrame (People_List,columns=['First_Name','Last_Name','Age'])
print (df)

And this is the result that you’ll get:

Convert List of lists to Dataframe

Alternatively, you could have your list of lists as follows:

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

So the Python code, to perform the conversion to the DataFrame, would look like this:

from pandas import DataFrame

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

df = DataFrame (People_List).transpose()
df.columns = ['First_Name','Last_Name','Age']
print (df)

Run the code, and you’ll get the same DataFrame:

Convert List of lists to Dataframe

Check the Object Type

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

from pandas import DataFrame

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

df = DataFrame (People_List).transpose()
df.columns = ['First_Name','Last_Name','Age']

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:

from pandas import DataFrame

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

df = DataFrame (People_List).transpose()
df.columns = ['First_Name','Last_Name','Age']

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))

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

Stats pandas

Different Representation of the DataFrame

Finally, you may apply the following code to represent your DataFrame:

from pandas import DataFrame

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

df = DataFrame (People_List, index = ['First_Name','Last_Name','Age'],columns = ['a','b','c','d','e'])
print (df)

This is the representation that you’ll get:

Horizontal view of data

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 the steps to perform the conversion.