Notice: Function register_block_script_handle was called incorrectly. The asset file (/home/beaczwhx/dggulaitutorial.com/wp-content/plugins/seo-by-rank-math/includes/modules/schema/blocks/faq/assets/js/index.asset.php) for the "editorScript" defined in "rank-math/faq-block" block definition is missing. Please see Debugging in WordPress for more information. (This message was added in version 5.5.0.) in /home/beaczwhx/dggulaitutorial.com/wp-includes/functions.php on line 6031

Notice: Function register_block_script_handle was called incorrectly. The asset file (/home/beaczwhx/dggulaitutorial.com/wp-content/plugins/seo-by-rank-math/includes/modules/schema/blocks/howto/assets/js/index.asset.php) for the "editorScript" defined in "rank-math/howto-block" block definition is missing. Please see Debugging in WordPress for more information. (This message was added in version 5.5.0.) in /home/beaczwhx/dggulaitutorial.com/wp-includes/functions.php on line 6031

Notice: Function register_block_script_handle was called incorrectly. The asset file (/home/beaczwhx/dggulaitutorial.com/wp-content/plugins/seo-by-rank-math/includes/modules/schema/blocks/schema/assets/js/index.asset.php) for the "editorScript" defined in "rank-math/rich-snippet" block definition is missing. Please see Debugging in WordPress for more information. (This message was added in version 5.5.0.) in /home/beaczwhx/dggulaitutorial.com/wp-includes/functions.php on line 6031
How To Create Pandas Dataframe From List In Python - Dggul AI Tutorial

How To Create Pandas Dataframe From List In Python


Notice: Undefined index: limited_lang in /home/beaczwhx/dggulaitutorial.com/wp-content/plugins/code-syntax-highlighter/inc/src/rendrer.php on line 297

Welcome to my new How To Create A Dataframe From A List In Python tutorial. In this tutorial, you will learn to create pandas dataframe from list in python.

We have various ways to create pandas dataframe from a list such as from simple list, list of lists, list of dicts, and list of tuple.  We can easily create pandas dataframe from a list in python by just passing list object to dataframe constructor(provided by pandas library).

These ways are very simple and easy to convert list to dataframe in python. So keep reading this article to know the answer of How Do You Create a Dataframe From a List In Python.

But before starting this tutorial if you haven’t checked previous pandas tutorial then check them first to understand basics of pandas and dataframe.

What Is Pandas In Python

How To Create A Dataframe From A List In Python

Are you excited to learn these ways to create dataframe in python from list? So without wasting the time, let’s get started.

Pandas Dataframe from A Simple List

Creating a dataframe from a simple list is very easy. Let’s take an example, we have a list of integers and now we have to create dataframe from it. So to create dataframe from this list we have to pass this list object to the dataframe constructor. See the below code.

import pandas as pd
my_list=[4,6,2]
df_list=pd.DataFrame(my_list)
print(df_list)

Now run this code on your IDE(I am using Jupyter Notebook). So the output of this code is as follows.

How To Create A Dataframe From A List In Python

Pandas Dataframe From A Simple List With Index And Column Names

So guys, now we want to create dataframe with index and column names. In this case, we just have to specify column names and index as attributes. Now run the following code.

import pandas as pd
my_list=[4,6,2]
df_list=pd.DataFrame(my_list, columns=['Number'], index=[1,2,3])
print(df_list)

Now let’s check the output. So the output is as follows.

How To Create A Dataframe From A List In PythonPandas Dataframe From List Of Dicts

Some times we have list of dictionaries that means a list having dictionaries values. For example, we have following list of dictionaries.

list_of_dict=[{'Name':'John', 'Roll':3},{'Name':'Adom', 'Roll':53},{'Name':'James', 'Roll':90}]

And we have to create dataframe from this list of dict. So let’s see how to do that.

import pandas as pd
list_of_dict=[{'Name':'John', 'Roll':3},{'Name':'Adom', 'Roll':53},{'Name':'James', 'Roll':90}]
df_list=pd.DataFrame(list_of_dict,index=[1,2,3])
print(df_list)

Run the above code and check the output. So its output is as follows.

How To Create A Dataframe From A List In Python

In the above output, you can see keys of dictionaries is treated as columns of dataframe.

In the above example we have list of dictionaries having common key. But if there is a case where we have a list of dictionaries having different keys then how to create dataframe from it. So let’s understand this with an example. Let’s say we have following list of dictionaries.

list_of_dict=[{'Name':'John', 'Roll':3},{'Name':'Adom', 'Marks':500},{'Name':'James', 'Roll':90}]

Let’s create dataframe from this list of dict. Run the following code.

import pandas as pd
list_of_dict=[{'Name':'John', 'Roll':3},{'Name':'Adom', 'Marks':500},{'Name':'James', 'Roll':90}]
df_list=pd.DataFrame(list_of_dict,index=[1,2,3])
print(df_list)

Lets check its output.

How To Create A Dataframe From A List In Python

As shown in output, three columns are created in this dataframe. First dictionary doesn’t have a Marks key that’s why in the dataframe the first row has no marks value and that’s why it is printing NaN. In this way, where key value doesn’t exist, NaN value is printing.

Also Read : How To Create Pandas Series In Python

So guys till now you have learned two ways to create dataframe from list. I am pretty sure this how to create a dataframe from a list in python tutorial is helping you to short out your queries. So KEEP READING to know another ways to create dataframe from list.

Pandas Dataframe From List Of Lists

Now we will create dataframe from list of lists. So let’s consider we have following list of lists.

list_of_lists=[[90,2,50,80],[20,60,30,40],[35,83,65,10]]

Now let’s create dataframe from this list of lists. So write and run the following code snippet.

import pandas as pd
list_of_lists=[[90,2,50,80],[20,60,30,40],[35,83,65,10]]
df_list=pd.DataFrame(list_of_lists,index=[1,2,3],columns=['a','b','c','d'])
print(df_list)

And its output is as follows.

Pandas Dataframe From Two Lists

If we have two lists and have to create dataframe from these lists. So to create dataframe from two lists, firstly we combine two lists using zip() method. Then pass this list to dataframe constructor.

import pandas as pd
List1=['Goyal','sandy','Helen']
List2=[30,90,80]
zlist=list(zip(List1,List2))
df_list=pd.DataFrame(zlist,index=[1,2,3],columns=['Name','Roll'])
print(df_list)

So on running this code we get following output.

Pandas dataframe from List Of Tuples

In order to create dataframe from list of tuples, we have to pass list of tuples object to the dataframe constructor. Let’s see how to do that.

import pandas as pd 
list_of_tuples=[(90,'Hello',80),(100,'World',20),(70,'Game',89)]
df_list=pd.DataFrame(list_0f_tuples,index=[1,2,3])
print(df_list)

Run the above code and check the output.

So guys I am wrapping up this How To Create A Dataframe From A List In Python tutorial here. You have learned multiple ways to create dataframe list in python. So I hope you found this tutorial very helpful. If so then don’t forget to share this post with your fellow programmers. And if you faced any difficulty while following this tutorial then drop your queries in comment section. I will be happy to solve your questions. You will get other helpful pandas tutorial in upcoming tutorial, till then stay tuned with Dggul AI Tutorial.

Leave a Comment