In this article we will discuss how to convert a single or multiple lists to a DataFrame.

Python’s pandas library provide a constructor of DataFrame to create a Dataframe by passing objects i.e.

Here data parameter can be a numpy ndarray , dict, or an other DataFrame. Also, columns and index are for column and index labels.
Let’s use this to convert lists to dataframe object from lists.

Create DataFrame from list of lists

Suppose we have a list of lists i.e.

Pass this list to DataFrame’s constructor to create a dataframe object i.e.

Contents of the created DataFrames are as follows,

Create DataFrame from lists of tuples

Just like list of lists we can pass list of tuples in dataframe contsructor to create a dataframe.

Suppose we have a list of tuples i.e.

Pass this list of tuples to DataFrame’s constructor to create a DataFrame object i.e.

Contents of the created dataframe is as follows,

Both Column & Index labels are default. But we can also provide them i.e.

Create a Dataframe from list and  set column names and indexes

Contents of the created dataframe is as follows,

Create dataframe from list of tuples and skip certain columns

In out list of tuples we have 3 entries in each tuple. What if we want to use 1st and 3rd entry only?
Let’s create a dataframe by skipping 2nd entry in tuples i.e.

Contents of the created dataframe is as follows,

Create dataframe from multiple lists

Suppose we have 3 different lists i.e.

Now we want to conver them to a dataframe with each lists as a column. Let’s see how to do that i.e.
Zip the lists to create a list of tuples i.e.

Contents of ziipledLists is,

Let’s create a dataframe with this zipped lists i.e.

Contents of the created dataframe is as follows,

Complete example is as follows,

Output:

 

Click Here to Subscribe for more Articles / Tutorials like this.