create pandas dataframe from dictionary

How to Create Pandas Dataframe from Dictionary

Python Pandas allows you to easily analyze and process data using DataFrames. There are several ways to load data into DataFrames. In this article, we will learn how to create Pandas DataFrame from Dictionary.


How to Create Pandas Dataframe from Dictionary

We will look at several easy ways to create dataframe from dictionary.


1. Basic Example

The simplest way to create pandas dataframe from dictionary is to directly pass it in dataframe constructor function. In the following example, we pass a dictionary with 3 keys Name, Age, University and each one having a list of values.

# import pandas library
import pandas as pd

# dictionary with list object in values
details = {
	'Name' : ['John', 'Jim', 'Shawn', 'Tim'],
	'Age' : [23, 21, 22, 21],
	'University' : ['BHU', 'JNU', 'DU', 'BHU'],
}

# creating a Dataframe object
df = pd.DataFrame(details)

df

In this case, pandas will automatically parse the dictionary such that dictionary key values become column names, the 1st element of dataframe consists of 1st elements of each list, and so on. So your dictionary will look like the following.

  Name Age University
0 John  23  BHU
1 Jim   21  JNU
2 Shawn 22  DU
4 Tim   21  BHU


2. Custom Indexes

In the above example, each dataframe element has a system generated index such as 0, 1, 2, … You can also set custom index values for each dataframe element by mentioning it in indexes argument.

# import pandas library
import pandas as pd

# dictionary with list object in values
details = {
	'Name' : ['John', 'Jim', 'Shawn', 'Tim'],
	'Age' : [23, 21, 22, 21],
	'University' : ['BHU', 'JNU', 'DU', 'BHU'],
}

# creating a Dataframe object from dictionary
# with custom indexing
df = pd.DataFrame(details, index = ['a', 'b', 'c', 'd'])

df

Now your dataframe will look like the following. Notice the custom index values a, b, c, d on left of table below.

  Name Age University
a John  23  BHU
b Jim   21  JNU
c Shawn 22  DU
d Tim   21  BHU


3. Using Simple Dictionary

You can also use simple dictionary to create dataframes. In this case, each key-value pair of dictionary becomes a row of dataframe, with key and value in separate columns.

Here is an example of the same. In this case, we simply pass a list of tuples. We use details.items() function to get the key-value pairs as a list of tuples.

# import pandas library
import pandas as pd

# dictionary
details = {
	'John' : 22,
	'Jane' : 21,
	'Jim' : 23
	}

# creating a Dataframe object from a list
# of tuples of key, value pair
df = pd.DataFrame(list(details.items()))

df

Now your dataframe will look like.

  0    1
0 John 22
1 Jane 21
2 Jim  23

In this case, you will notice that the dictionary keys have been used to popular the 1st column, instead being used dataframe column names. The values are used to populate the second column.


4. Using Different Orientation

You can also create python dataframe with different orientation by using the orient column.

# import pandas library
import pandas as pd

# dictionary with list object in values
details = {
	'Name' : ['John', 'Jim', 'Shawn', 'Tim'],
	'Age' : [23, 21, 22, 21],
	'University' : ['BHU', 'JNU', 'DU', 'BHU'],
}

# creating a Dataframe object in which dictionary
# key is act as index value and column value is
# 0, 1, 2...
df = pd.DataFrame.from_dict(details, orient = 'index')

df

In this case, your dataframe will look like the following.

             0      1           2        3
Name         Ankit  Aishwarya   Shaurya  Shivangi
Age          23     21          22       21
University   BHU    JNU         DU       BHU

In this article, we have learnt how to create python pandas dataframe using dictionaries. You can modify it as per your requirement.

Also read:

How to Create Pandas DataFrame from Dictionary
How to Access Index of Last Element in DataFrame
MySQL Insert or Update If Exists
How to Do Regular Expression Replace in MySQL
How to Get Digits from String in MySQL

Leave a Reply

Your email address will not be published. Required fields are marked *