Convert a List to Pandas Dataframe (with examples)

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

You may then use the following 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 into a list. If that’s the case, you may want to check the following guide that explains the steps to perform the conversion.

2 thoughts on “Convert a List to Pandas Dataframe (with examples)”

    • Thanks, much appreciated!

      Reply

Leave a Comment