Can we convert list to 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: 
df: 

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.

You can directly call the pd.DataFrame[] method and pass your list as the parameter.

import pandas as pd
l = ['Thanks You','Its fine no problem','Are you sure']
pd.DataFrame[l]

Output:

                      0
0            Thanks You
1   Its fine no problem
2          Are you sure

And if you have multiple lists and you want to make a dataframe out of it. You can do it as following:

import pandas as pd
names =["A","B","C","D"]
salary =[50000,90000,41000,62000]
age = [24,24,23,25]
data = pd.DataFrame[[names,salary,age]] #Each list would be added as a row
data = data.transpose[] #To Transpose and make each rows as columns
data.columns=['Names','Salary','Age'] #Rename the columns
data.head[]

Output:

    Names   Salary  Age
0       A    50000   24
1       B    90000   24
2       C    41000   23
3       D    62000   25

How do I convert a list to a DataFrame in Python?

Use pandas. DataFrame[] constructor to convert a list to a DataFrame. Use pandas. DataFrame[data, columns] to convert a list to a DataFrame.

Can we create DataFrame from list?

The pandas DataFrame can be created by using the list of lists, to do this we need to pass a python list of lists as a parameter to the pandas. DataFrame[] function. Pandas DataFrame will represent the data in a tabular format, like rows and columns.

Can you put a list in a DataFrame Python?

You can insert a list of values into a cell in Pandas DataFrame using DataFrame.at[] , DataFrame. iat[] , and DataFrame.

How do I turn a list into a DataFrame row?

Method 1: Using T function This is known as the Transpose function, this will convert the list into a row. Here each value is stored in one column. Example: Python3.

Chủ Đề