Write a Python program to convert the list to Pandas DataFrame with an example. In this example, first, we declared a fruit string list. Next, we used the pandas DataFrame function that converts the list to DataFrame.
import pandas as pd fruitList = ['kiwi', 'orange', 'banana', 'berry', 'mango', 'cherry'] print("List Items = ", fruitList) df = pd.DataFrame(fruitList) print(df)
List Items = ['kiwi', 'orange', 'banana', 'berry', 'mango', 'cherry']
0
0 kiwi
1 orange
2 banana
3 berry
4 mango
5 cherry
The 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.
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 Items = ['kiwi', 'orange', 'banana', 'berry', 'mango', 'cherry']
Fruits
1 kiwi
2 orange
3 banana
4 berry
5 mango
6 cherry
How to Convert Python List To Pandas DataFrame?
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.
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 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 DataFrame function also converts the nested lists.
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)
This nested one is different from others, and if you convert this nested list to DataFrame, you get the below result.
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)
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.
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 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