Convert List to Pandas Dataframe : Various Approaches

Convert List to Pandas Dataframe

In most cases, We use a list and in some cases, you have to convert the list to pandas dataframe. If it is that case then you have come to the right place. Here in this tutorial, I will show you all the examples of the list to pandas dataframe conversion.

Steps by Steps to convert list to pandas dataframe

Step 1: Import all the necessary libraries.

Here I am using only Pandas libraries. So let’s import them.

import panadas as pd

Step 2: Follow all the below examples

Example 1: Convert a Simple list to dataframe

In this example, I will create a simple list. After that, I will convert it to dataframe using the pandas.DataFrame() method, Execute the following code.

country_list = ["India","USA","UK","Canada","Australia"]
df = pd.DataFrame(country_list,columns=["Country"])

Below is the output you will get after conversion.

Converting simple list to dataframe
Converting a simple list to dataframe

Example 2: Convert a list of list or 2D List to Dataframe.

Now I will show you how to convert a list of lists to dataframe. The method of conversion will be the same that is pandad.Dataframe().

Method 1:  Using a list of list

In this method, I will create a list of country_list with the country name, capital, and dialing code as a group. After that pass it to the DataFrame() method.

Just run the code below to achieve the results.

country_list = [["India","New Delhi",91],["USA","Washington D.C.",1],["UK","London",44],["Canada","Ottawa",1]]
df = pd.DataFrame(country_list,columns=["Country","Capital","Dialing Code"])

Here I am also passing the list of columns inside the DataFrame() method. In the next section, you will learn the second method.

You will get the output as below.

Convert a list of list or 2D Lise to Dataframe using list of list
Converting a list of list to Dataframe using simple method

Method 2:  Using transpose() method.

You already know the list of list act as matrix form if you convert them to dataframe. In this example I am first creating a name of countries in one list, then a list of its capital, and so on. Then you will convert it to dataframe and transpose it to get the desire results.

Execute the below code.

country_list = [["India","USA","UK","Canada","Australia"],["New Delhi","Washington D.C.","London","Ottawa"],[91,1,44,1]]
df = pd.DataFrame(country_list)
df2 = df.transpose()
df2.columns = ["Country","Capital","Dialing Code"]

Here is the output you will get.

Convert a list of list or 2D Lise to Dataframe using transpose() method
Converting a list of list  Dataframe using transpose() method

 

Example 3: Convert a list of dictionaries to pandas dataframe

Example 2 was using a list of lists. But here in this example, I will show you how to convert a list of dictionaries to pandas dataframe.

But for that let’s create a sample list of dictionaries. Use the below code.

country_dict = [{"name":"India","capital":"New Delhi","dialing_code":91},
                {"name":"USA","capital":"Washington D.C","dialing_code":1}
                ,{"name":"UK","capital":"London","dialing_code":44},
                {"name":"Canada","capital":"Ottawa","dialing_code":1}]

To convert it to dataframe just pass it to the DataFrame().

df = pd.DataFrame(country_dict)

Below is the desired output.

Converting list of dictionaries to pandas dataframe
Converting list of dictionaries to pandas dataframe

You will notice in the above image that the key pair has become the column name of the dataframe and the corresponding value becomes the row.

Source:

Offical pandas.DataFrame Method

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner