{"id":7445,"date":"2021-05-30T09:15:44","date_gmt":"2021-05-30T03:45:44","guid":{"rendered":"https:\/\/python-programs.com\/?p=7445"},"modified":"2021-11-22T18:53:35","modified_gmt":"2021-11-22T13:23:35","slug":"pandas-create-dataframe-from-list-of-dictionaries","status":"publish","type":"post","link":"https:\/\/python-programs.com\/pandas-create-dataframe-from-list-of-dictionaries\/","title":{"rendered":"Pandas: Create Dataframe from List of Dictionaries"},"content":{"rendered":"
In this article, we discuss different methods by which we can create a dataframe from a list of dictionaries. Before going to the actual article let us done some observations that help to understand the concept easily. Suppose we have a list of dictionary:-<\/p>\n
Here we know that dictionaries consist of key-value pairs. So we can analyze that if we make the key as our column name and values as the column value then a dataframe is easily created. And we have a list of dictionaries so a dataframe with multiple rows also.<\/p>\n This methods helps us to create dataframe in python<\/p>\n As we see in in pandas.Datframe() method there is parameter name data.We have to simply pass our list of dictionaries in this method and it will return the dataframe.Let see this with the help of an example.<\/p>\n Output<\/p>\n Here we see that dataframe is created with default indexes 0,1,2,3….<\/p>\n Now a question may arise if from any dictionary key-value pair is less than other dictionaries.So in this case what happened.Let understand it with the help of an example.<\/p>\n Output<\/p>\n Here we see in case of missing key value pair NaN value is there in the output.<\/p>\n Unlike the previous method where we have default indexes we can also give custom indexes by passes list of indexes in index parameter of pandas.DataFrame() function.Let see this with the help of an example.<\/p>\n Output<\/p>\n Here we see that instead of default index 1,2,3….. we have now indes a,b,c,d.<\/p>\n With the help of pandas.DataFrame() method we can easily arrange order of column by simply passes list ozf columns in columns parameter in the order in which we want to display it in our dataframe.Let see this with the help of example.<\/p>\n Output<\/p>\n Here also a question may arise if we pass less column in columns parameter or we pass more column in parameter then what happened.Let see this with the help of an example.<\/p>\n In this case the column which we don’t pass will be drop from the dataframe.Let see this with the help of an example.<\/p>\n Output<\/p>\n Here we see that we didn’t pass Age column that’s why Age clumn is also not in our dataframe.<\/p>\n In this case a new column will be added in dataframe but its all the value will be NaN.Let see this with the help of an example.<\/p>\n Output<\/p>\n So these are the methods to create dataframe from list of dictionary in pandas.<\/p>\n","protected":false},"excerpt":{"rendered":" Methods of creating a dataframe from a list of dictionaries In this article, we discuss different methods by which we can create a dataframe from a list of dictionaries. Before going to the actual article let us done some observations that help to understand the concept easily. Suppose we have a list of dictionary:- list_of_dict …<\/p>\nlist_of_dict = [
\n{'Name': 'Mayank' , 'Age': 25, 'Marks': 91},
\n{'Name': 'Raj', 'Age': 21, 'Marks': 97},
\n{'Name': 'Rahul', 'Age': 23, 'Marks': 79},
\n{'Name': 'Manish' , 'Age': 23},
\n]<\/code><\/p>\npandas.DataFrame<\/h3>\n
syntax: pandas.<\/span>DataFrame<\/span>(<\/span>data=<\/span>None<\/span>, index=<\/span>None<\/span>, columns=<\/span>None<\/span>, dtype=<\/span>None<\/span>, copy=<\/span>False<\/span>)<\/span><\/code><\/p>\n
Let us see different methods to create dataframe from a list of dictionaries<\/h3>\n
\n
Method 1-Create Dataframe from list of dictionaries with default indexes<\/h3>\n<\/li>\n<\/ul>\n
import pandas as pd\r\nimport numpy as np\r\n\r\nlist_of_dict = [\r\n {'Name': 'Mayank' , 'Age': 25, 'Marks': 91},\r\n {'Name': 'Raj', 'Age': 21, 'Marks': 97},\r\n {'Name': 'Rahul', 'Age': 23, 'Marks': 79},\r\n {'Name': 'Manish' , 'Age': 23, 'Marks': 86},\r\n]\r\n#create dataframe\r\ndf=pd.DataFrame(list_of_dict)\r\nprint(df)<\/pre>\n
Age Marks Name\r\n0 25 91 Mayank\r\n1 21 97 Raj\r\n2 23 79 Rahul\r\n3 23 86 Manish<\/span><\/pre>\n
import pandas as pd\r\nimport numpy as np\r\n\r\nlist_of_dict = [\r\n {'Name': 'Mayank' , 'Age': 25, 'Marks': 91},\r\n {'Name': 'Raj', 'Age': 21, 'Marks': 97},\r\n {'Name': 'Rahul', 'Marks': 79},\r\n {'Name': 'Manish' , 'Age': 23},\r\n]\r\n#create dataframe\r\ndf=pd.DataFrame(list_of_dict)\r\nprint(df)<\/pre>\n
Age Marks Name\r\n0 25.0 91.0 Mayank\r\n1 21.0 97.0 Raj\r\n2 NaN 79.0 Rahul\r\n3 23.0 NaN Manish<\/span><\/pre>\n
\n
Method 2- Create Dataframe from list of dictionary with custom indexes<\/h3>\n<\/li>\n<\/ul>\n
import pandas as pd\r\nimport numpy as np\r\n\r\nlist_of_dict = [\r\n {'Name': 'Mayank' , 'Age': 25, 'Marks': 91},\r\n {'Name': 'Raj', 'Age': 21, 'Marks': 97},\r\n {'Name': 'Rahul', 'Marks': 79},\r\n {'Name': 'Manish' , 'Age': 23},\r\n]\r\n#create dataframe\r\ndf=pd.DataFrame(list_of_dict,index=['a','b','c','d'])\r\nprint(df)<\/pre>\n
Age Marks Name\r\na 25.0 91.0 Mayank\r\nb 21.0 97.0 Raj\r\nc NaN 79.0 Rahul\r\nd 23.0 NaN Manish<\/span><\/pre>\n
\n
Method 3-Create Dataframe from list of dictionaries with changed order of columns<\/h3>\n<\/li>\n<\/ul>\n
import pandas as pd\r\nimport numpy as np\r\n\r\nlist_of_dict = [\r\n {'Name': 'Mayank' , 'Age': 25, 'Marks': 91},\r\n {'Name': 'Raj', 'Age': 21, 'Marks': 97},\r\n {'Name': 'Rahul', 'Age': 23, 'Marks': 79},\r\n {'Name': 'Manish' , 'Age': 23, 'Marks': 86},\r\n]\r\n#create dataframe\r\ndf=pd.DataFrame(list_of_dict,columns=['Name', 'Marks', 'Age'])\r\nprint(df)<\/pre>\n
Name Marks Age\r\n0 Mayank 91 25\r\n1 Raj 97 21\r\n2 Rahul 79 23\r\n3 Manish 86 23<\/span><\/pre>\n
Case 1: Less column in column parameter<\/h3>\n
import pandas as pd\r\nimport numpy as np\r\n\r\nlist_of_dict = [\r\n {'Name': 'Mayank' , 'Age': 25, 'Marks': 91},\r\n {'Name': 'Raj', 'Age': 21, 'Marks': 97},\r\n {'Name': 'Rahul', 'Age': 23, 'Marks': 79},\r\n {'Name': 'Manish' , 'Age': 23, 'Marks': 86},\r\n]\r\n#create dataframe\r\ndf=pd.DataFrame(list_of_dict,columns=['Name', 'Marks'])\r\nprint(df)<\/pre>\n
Name Marks\r\n0 Mayank 91\r\n1 Raj 97\r\n2 Rahul 79\r\n3 Manish 86<\/span><\/pre>\n
Case 2: More column in column parameter<\/h3>\n
import pandas as pd\r\nimport numpy as np\r\n\r\nlist_of_dict = [\r\n {'Name': 'Mayank' , 'Age': 25, 'Marks': 91},\r\n {'Name': 'Raj', 'Age': 21, 'Marks': 97},\r\n {'Name': 'Rahul', 'Age': 23, 'Marks': 79},\r\n {'Name': 'Manish' , 'Age': 23, 'Marks': 86},\r\n]\r\n#create dataframe\r\ndf=pd.DataFrame(list_of_dict,columns=['Name', 'Marks', 'Age','city'])\r\nprint(df)<\/pre>\n
Name Marks Age city\r\n0 Mayank 91 25 NaN\r\n1 Raj 97 21 NaN\r\n2 Rahul 79 23 NaN\r\n3 Manish 86 23 NaN<\/span><\/pre>\n