In this, we will explore how to Convert Lists to Pandas DataFrame with example.
The DataFrame is a two-dimensional tabular data structure like SQL Database, Excel, CSV data is oriented in rows and columns form. At times we need to convert the data collections like lists, dictionaries to dataframe that we are going to explore in this post. We will learn 8 ways in Python Pandas to Convert Lists to DataFrame that includes single list, multiple lists, list of lists, list of tuples.
Pandas Dataframe() constructor
Python Pandas library provides a Dataframe() constructor that allows us to create a data frame.
Syntax
pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
Parameters :
The parameter data can be ndarray,dict,list. The columns to set columns name and indexes,we can specify dtype while creating a data frame.
1. dataframe() to convert List to DataFrame
In this example, we have a single mixed datatype list that we are passing to Dataframe() constructor to create a pandas dataframe.
import pandas as pd
# list of mixed datatype
list = [12,14,15,'Math', 'English', 'Sci', 'Music']
df = pd.DataFrame(list)
print(df)
Output
0
0 12
1 14
2 15
3 Math
4 English
5 Sci
6 Music
2. Convert List of lists to DataFrame
In this example, we have a list of lists(subjects). We are passing this nested list to DataFrame() constructor and specifying the column name to create a dataframe.
import pandas as pd
# a List of lists
subjects = [ ['Jack','Math',100] ,
['Ron','Music', 100],['Tom','chem',100 ] ]
df = pd.DataFrame(subjects,columns =['Name','Subjects','Marks'])
print(df)
Output
Name Subjects Marks
0 Jack Math 100
1 Ron Music 100
2 Tom chem 100
3. Zip() to Convert multiple list to DataFrame
In this example, we have multiple lists (list_names, list_subjects, list_marks). All multiple lists are passed as an argument to Zip() method to create a single list.
The zip list(zip_list) passed to DataFrame() constructor and specifying the column name to create a dataframe.
import pandas as pd
# a List of lists
list_names =['Jack','Ron','Tom']
list_subjects =['Math','Music','chem']
list_marks = [100,100,100]
zip_list = list(zip(list_names, list_subjects, list_marks))
df = pd.DataFrame(zip_list, columns = ['Name','Subject' , 'Marks'], index=['Row_1', 'Row_2', 'Row_3'])
print(df)
Output
Name Subject Marks
Row_1 Jack Math 100
Row_2 Ron Music 100
Row_3 Tom chem 100
4. Convert List to dataframe set columns name and indexes
Both column’s name and indexes are Default. But we need to customize the dataframe as per our requirement we can specify them as shown in the below example.
import pandas as pd
# a List of lists
subjects = [ ['Jack','Math',100] ,
['Ron','Music', 100],['Tom','chem',100 ] ]
df = pd.DataFrame(subjects, columns = ['Name','Subject' , 'Marks'], index=['Row_1', 'Row_2', 'Row_3'])
print(df)
Output
Name Subject Marks
Row_1 Jack Math 100
Row_2 Ron Music 100
Row_3 Tom chem 100
5. Convert List of lists to DataFrame exclude specific column
At times we need to exclude the column while creating a dataframe that column name can be specified by using from_records() method
import pandas as pd
# a List of lists
subjects = [ ['Jack','Math',100] ,
['Ron','Music', 100],['Tom','chem',100 ] ]
df = pd.DataFrame.from_records(subjects,exclude=['Subject'], columns = ['Name','Subject' , 'Marks'], index=['Row_1', 'Row_2', 'Row_3'])
print(df)
Output
Name Marks
Row_1 Jack 100
Row_2 Ron 100
Row_3 Tom 100
6. Convert Mutidimesional list to Dataframe specify a dtype
In this example, we can specify the dtype while creating a dataframe from the Multidimensional list.
import pandas as pd
# a List of lists
subjects = [ ['Jack','Math',100] ,
['Ron','Music', 100],['Tom','chem',100 ] ]
df = pd.DataFrame(subjects, columns = ['Name','Subject' , 'Marks'], index=['Row_1', 'Row_2', 'Row_3'],dtype = float)
print(df)
Output
Name Subject Marks
Row_1 Jack Math 100.0
Row_2 Ron Music 100.0
Row_3 Tom chem 100.0
7.Convert list of tuples to DataFrame
A list of tuples can be converted to a dataframe As we are passing to the dataframe() constructor to get a dataframe.
import pandas as pd
subjects = [ ('Jack','Math',100) ,
('Ron','Music', 100),('Tom','chem',100 ) ]
df = pd.DataFrame(subjects,columns =['Name','Subjects','Marks'])
print(df)
Output
Name Subjects Marks
0 Jack Math 100
1 Ron Music 100
2 Tom chem 100
8. Create a dataframe from lists in dictionary
We can convert multiple lists in the dictionary into a dataframe using the Dataframe() constructors.
Let us understand with this example how to accomplish this.
import pandas as pd
list_names =['Jack','Ron','Tom']
list_subjects =['Math','Music','chem']
list_marks = [100,100,100]
lists_in_dict = {'Name': list_names, 'Subjects': list_subjects, 'Marks': list_marks}
df = pd.DataFrame(lists_in_dict)
print(df)
Output
Name Subjects Marks
0 Jack Math 100
1 Ron Music 100
2 Tom chem 100
Summary:
We have explored multiple ways in his post about Python Pandas Convert Lists to DataFrame that includes single list, multiple lists, list of lists, list of tuples.