Convert Python List To Pandas DataFrame

Write a Python program to convert the list to Pandas DataFrame with an example. In this example, first, we declared a fruit list (string list). Next, we used the pandas DataFrame function that converts the list to DataFrame.

# Create Pandas Dataframe from List
import pandas as pd

fruitList = ['kiwi', 'orange', 'banana', 'berry', 'mango', 'cherry']
print("List Items = ", fruitList)

df = pd.DataFrame(fruitList)
print(df)

List to pandas DataFrame output

List Items =  ['kiwi', 'orange', 'banana', 'berry', 'mango', 'cherry']
        0
0    kiwi
1  orange
2  banana
3   berry
4   mango
5  cherry

The Python DataFrame has a column that helps to assign the column names and index to assign the index values. Here, we set the column name as Fruits and index values as a list of six integers.

# Create Pandas Dataframe from List

import pandas as pd

fruitList = ['kiwi', 'orange', 'banana', 'berry', 'mango', 'cherry']

print("List Items = ", fruitList)

df = pd.DataFrame(fruitList, columns = ['Fruits'],
                  index = [1, 2, 3, 4, 5, 6])

print(df)

List to DataFrame output

List Items =  ['kiwi', 'orange', 'banana', 'berry', 'mango', 'cherry']
   Fruits
1    kiwi
2  orange
3  banana
4   berry
5   mango
6  cherry

Create Pandas DataFrame from List in Python

We declared two lists of fruits and orders. Next, we used the zip function to zip those two lists, and then the pandas DataFrame function will convert them to a DataFrame.

# Create Pandas Dataframe from List

import pandas as pd

fruitList = ['kiwi', 'orange', 'banana', 'berry', 'mango', 'cherry']

print("List Items = ", fruitList)

ordersList = [100, 2000, 5000, 400, 8000, 12000]

print("List Items = ", ordersList)

df = pd.DataFrame(list(zip(fruitList, ordersList)),
                  columns = ['Fruits', 'Orders'],
                  index = ['a', 'b', 'c', 'd', 'e', 'f'])

print(df)

List to DataFrame output

List Items =  ['kiwi', 'orange', 'banana', 'berry', 'mango', 'cherry']
List Items =  [100, 2000, 5000, 400, 8000, 12000]
   Fruits  Orders
a    kiwi     100
b  orange    2000
c  banana    5000
d   berry     400
e   mango    8000
f  cherry   12000

The Pandas DataFrame function also converts the nested lists or list of lists into a DataFrame.

# Create Pandas Dataframe from List

import pandas as pd

fruitList = [['kiwi', 100], ['orange', 3500], ['banana', 2000],
             ['berry', 1500], ['mango', 5000],['cherry', 9000]]

print("List Items = ", fruitList)

df = pd.DataFrame(fruitList, columns = ['Fruits', 'Orders'])

print(df)
Convert Python List To Pandas DataFrame 4

This nested list is different from others, and if you convert this nested list to pandas DataFrame, you get the below result.

# Create Pandas Dataframe from List
import pandas as pd

emplist = [['Jhon', 'Xi', 'Ram', 'Dave', 'Nancy'],
           ['Miller', 'Jing', 'KK', 'Williams', 'Cook'],
           [40, 52, 25, 37, 29]]

df = pd.DataFrame(emplist)
print(df)

print('\n========')
df1 = pd.DataFrame(emplist, index = ['First_Name', 'Last_Name', 'Age'])
print(df1)

Nested List to DataFrame output

        0     1    2         3      4
0    Jhon    Xi  Ram      Dave  Nancy
1  Miller  Jing   KK  Williams   Cook
2      40    52   25        37     29

========
                 0     1    2         3      4
First_Name    Jhon    Xi  Ram      Dave  Nancy
Last_Name   Miller  Jing   KK  Williams   Cook
Age             40    52   25        37     29

However, there is a transpose function in pandas DataFrame that will convert the above nested Python list into a meaningful one.

# Create Pandas Dataframe from List

import pandas as pd

emplist = [['Jhon', 'Xi', 'Ram', 'Dave', 'Nancy'],
           ['Miller', 'Jing', 'KK', 'Williams', 'Cook'],
           [40, 52, 25, 37, 29]]

print("List Items = ", emplist)

df = pd.DataFrame(emplist).transpose()

df.columns = ['First Name', 'Last Name', 'Age']

print(df)

df1 = pd.DataFrame(emplist)

print(df1)

List to DataFrame output.

List Items =  [['Jhon', 'Xi', 'Ram', 'Dave', 'Nancy'], ['Miller', 'Jing', 'KK', 'Williams', 'Cook'], [40, 52, 25, 37, 29]]
  First Name Last Name Age
0       Jhon    Miller  40
1         Xi      Jing  52
2        Ram        KK  25
3       Dave  Williams  37
4      Nancy      Cook  29
        0     1    2         3      4
0    Jhon    Xi  Ram      Dave  Nancy
1  Miller  Jing   KK  Williams   Cook
2      40    52   25        37     29