Home Python Pandas DataFrame from Dict

Pandas DataFrame from Dict

Dictionaries in Python are data structures responsible for storing values as keys and values. To manipulate data for machine learning tasks, you must convert dictionaries to pandas dataframes. The from_dict() function is used to create a DataFrame from an array-like dict or a dict of dicts. In fact, we will demonstrate with examples the Pandas’ usefulness and power. Pandas can generate dataframes from various data structures without requiring you to write a lot of code. A dictionary is one of these data structures.

The following statement will convert a dictionary to a Pandas Dataframe.

rs_df = pd.DataFrame.from_dict(new_dict)

The syntax is as follows:

classmethod DataFrame.from_dict(data, orient='columns', dtype=None, columns=None)

A note about Pandas variants

Before you begin, make sure Python is at least version 3.7. The most recent Pandas version available with Python 3.9 is 1.4.2. However, other versions like 0.22, first supported in Python 3.4, do not enable choosing column names when building a dictionary in all cases.

Now, the next task is the creation of a new Python environment and install Pandas as follows if you are using virtualenv:

virtualenv py37  --python=python3.7
pip install pandas

Check out the Pandas version by running the following code snippet:

import pandas as pd
pd.__version__

Let us now look at converting a Python Dictionary to a Pandas Dataframe. Using the pd.DataFrame.from_dict() class-method, we can convert a dictionary to a pandas dataframe.

What is the process of converting a Dictionary to a Pandas DataFrame

In this section, you’ll learn the necessary steps in converting a dictionary to a DataFrame. Conversion of a dictionary to a Pandas DataFrame is vital, and we will use the following template:

import pandas as pd

code_dict = {key:value,key:value,key:value,...}
df = pd.DataFrame(list(code_dict.items()),columns = ['column1','column2'])

After checking the template above, we will now demonstrate the steps and then finally tie these together by using the template.

Step 1: Compile Data for the Dictionary

Begin by gathering information for your dictionary. For example, collect the following Item and Cost Price information:


Item

Cost Price
Toshiba Laptop
3500
LG Screen 980

POS App

1350

Tri-monitor

2550

Step 2: Make a Dictionary

Create the dictionary next. To create the dictionary in our example, use the following code:

code_dict = {'Toshiba Laptop':3500,'LG Screen':980,'POS App':1350,'Tri-monitor':2550}
print(code_dict)
print(type(code_dict))

When you run the code in Python, you will obtain the following dictionary:

Creating a Dictionary
Creating a Dictionary

The syntax print (type(code_dict)) was added at the end of the function to check that we did indeed get a dictionary.

Final Step: Conversion of the Dictionary to a DataFrame

Finally, we will use the template below to transform the dictionary into a DataFrame:

import pandas as pd

code_dict = {key:value,key:value,key:value,...}
rs_df = pd.DataFrame(list(code_dict.items()),columns = ['column1','column2'])

Here is the complete Python code for converting a dictionary to a DataFrame in our example:

import pandas as pd

code_dict = {'Toshiba Laptop':3500,'LG Screen':980,'POS App':1350,'Tri-monitor':2550}
rs_df = pd.DataFrame(list(code_dict.items()),columns = ['Item','Cost Price'])

print(rs_df)
print(type(rs_df))
Conversion of the Dictionary to a DataFrame

Conversion of the Dictionary to a DataFrame

To ensure that we received a DataFrame, the syntax print (type(rs_df)) was added at the bottom of the function.

Using from_Dict(), convert a Dictionary to a Pandas Dataframe

You can use the from_dict() function, and the dictionary keys can be oriented as columns or rows in the Pandas Dataframe. Let’s go into the specifics. The from_dict() method in Pandas Dataframe is used for creating a Pandas Dataframe from a Dictionary object. It takes the following:

  • Dictionary – Dictionary contains data for the dataframe. Mandatory creation.
  • orient – Defines the orientation of whether a dataframe’s dictionary must be rows or columns.
  • dtype – The datatype in the pandas dataframe.Optional. By default, the datatype of the data in the dictionary will be inferred.
  • columns – a list of column names, with the default being None. When orient=’index,’ utilize column labels. When used with orient=’columns’ or orient=’tight,’ it throws a ValueError since dictionary keys are used as column names in these circumstances.

The code below represents how to generate a pandas dataframe from a dictionary. To develop a pandas dataframe, it just uses the necessary parameter dictionary. Other choices are discussed in the following sections of this article.

You can use this approach when the dictionary values are in list form. If you don’t, you’ll get an index error if you use all scalar values. If your dictionary key values are not in list form, proceed to the next stage.

import pandas as pd
code_dict = {"Item.No.": [1], "Item":["Camera"], "Quantity": [17], "Cost": [2000]}
rs_df = pd.DataFrame.from_dict(code_dict)
print(rs_df)

The values in the dictionary are used to generate a pandas dataframe.

Using a List of Items, Convert a Dictionary to a Dataframe

You may convert a dictionary to a pandas dataframe using list(my_dict.items()) to create a list of Dictionary entries. You can also use the columns parameter to pass the column header values. When Should You Use This Method? You should use this method when the Dictionary keys’ values are not a list of values.

import pandas as pd
code_dict = {"Item.No.": [1], "Item":["Camera"], "Quantity": [17], "Cost": [2000]}
rs_df = pd.DataFrame(list(code_dict.items()), columns = ['Name','Value'])
print(rs_df)

When using this method, the pandas dataframe will be generated with the dictionary keys as rows.

Convert a dictionary to a dataframe with the keys as rows

This section will teach you how to convert a dictionary to a pandas dataframe, using dictionary keys as rows in the pandas dataframe. You may accomplish this by passing the orient = ‘index’ parameter to the from_dict() method, as seen below.

import pandas as pd
code_dict = {"Item.No.": [1], "Item":["Camera"], "Quantity": [17], "Cost": [2000]}
rs_df = pd.DataFrame.from_dict(code_dict, orient = 'index')
print(rs_df)

As seen below, the dataframe is constructed using the dictionary keys as rows.

Convert a Dictionary to a Dataframe, with the keys as columns

This section will teach you how to convert a dictionary to a pandas dataframe using Dictionary keys as columns. You may accomplish this by using the orient = ‘columns’ parameter to the from_dict() method, as seen below. It is the from_dict() method’s default behavior.

import pandas as pd
code_dict = {"Item.No.": [1], "Item":["Camera"], "Quantity": [17], "Cost": [2000]}
rs_df = pd.DataFrame.from_dict(code_dict, orient = 'columns')
print(rs_df)

As seen below, the dataframe is constructed using the dictionary keys as rows.

Convert a Dictionary to a Dataframe Using an Index

In this section, you’ll learn how to convert a dictionary to a pandas dataframe with an Index column. The set_index() method can be used to set the index. The following parameters are accepted by the set_index() function.

  • keys – the label to be used as the index (For each row).
  • drop – bool, the default value is True. After utilizing a column as an index column, you are free to remove it from the dataframe.
  • append – bool, false by default and indicates whether columns should be appended to an existing index.
  • inplace -bool, default False. If True, the DataFrame is modified in place. Essentially, that means there is no need to create a new object.

Verify integrity is a bool with the default value of False. Additionally, check for duplicates in the new index. Otherwise, postpone the payment until it is absolutely necessary. Setting this method to False will improve its performance. However, if this is set to True, an error is thrown if the index keys have duplicate values.

The code below is a classical representation of transforming a dictionary into a dataframe with an index column.

import pandas as pd
code_dict = {"Item.No.": [1], "Item":["Camera"], "Quantity": [17], "Cost": [2000]}
rs_df = pd.DataFrame(code_dict)
rs_df = df.set_index('Item.NO.')
print(rs_df)

The pandas dataframe is formed, and the column Item.No is utilized as the dataframe’s index column, as seen below.

Conversion of a dictionary to a dataframe without an index

In this section, you’ll learn how to convert a dictionary to a Pandas Dataframe without an index column. You can utilize the dictionary object directly.

import pandas as pd
code_dict = {"Item.No.": [1], "Item":["Camera"], "Quantity": [17], "Cost": [2000]}
rs_df = pd.DataFrame(code_dict)
print(rs_df)

Using Pandas DataFrame constructor to create a dataframe

In the following demo, we create a Pandas dataframe from a dictionary. We use the Pandas constructor since it can handle various data structures. The two keys in the dictionary below are sport and terms. Because each value is an array of four items, it naturally fits into what might be considered a table. The latter has two columns and four rows.

Pandas are intended to work with data in rows and columns. A row index is assigned to each row. The numerals, including 0, 1, 2, 3, and so forth, are used by default. However, it also allows you to use names. So, in the array idx, use the same.

import pandas as pd
dict =  {'sport': ["soccer", "basketball", "tennis", "athletics"],
'terms': ["shoot", "3-pointer", "hit", "laps"]}
str_idx = ['Green', 'White', 'Blue','Red']
rs_dp = pd.DataFrame(dict,index=str_idx)
print(rs_dp)

The generated dataframe is as follows:

using pandas dataframe constructor to create a dataframe
using pandas dataframe constructor to create a dataframe

Example 1: Using a list to pass the key value

import pandas as pd

vals_dic = {'name': ['Joy', 'Angelo', 'Green', 'James'],
		'age': ['81', '30', '8', '16']}
rs_vals = pd.DataFrame.from_dict(vals_dic)

print(rs_vals)

Example 2: Using a list to pass the key value

import pandas as pd


vals_dic = [{'area': 'New York', 'rainfall': 90, 'temperature': 30},
		{'area': 'London', 'rainfall': 48, 'temperature': 19},
		{'area': 'Manchester', 'rainfall': 189, 'temperature': 41 }]

rs_df = pd.DataFrame.from_dict(vals_dic)

print(rs_df)

Example 3: Using the orient argument to switch the dataframe’s orientation from column to index

import pandas as pd

vals_dic  ={'area': ['New York', 'London', 'Manchester'],
	'rainfall':[90, 48, 189],
	'temperature':[30, 19, 41]}

rs_df  = pd.DataFrame.from_dict(vals_dic, orient ='index')
print(rs_df )

Example 4: Using the orient argument to switch the dataframe’s orientation from column to index

import pandas as pd
code_dict = {"Item.No.": [1], "Item":["Camera"], "Quantity": [17], "Cost": [2000]}
rs_df = pd.DataFrame.from_dict(code_dict)
print(rs_df)

Examples 5: By default, the keys of the dict become DataFrame columns

import numpy as np
import pandas as pd

code_dict = {'d_one': [8, 7, 6, 0], 'd_two': ['a', 'b', 'c', 'd']}
pd.DataFrame.from_dict(code_dict)

Example 6: To build the DataFrame using dictionary keys as rows, specify orient=’index’

code_dict = {'d_one': [8, 7, 6, 0], 'd_two': ['a', 'b', 'c', 'd']}
pd.DataFrame.from_dict(code_dict, orient='index')

Example 7: Supplying column names manually when using the ‘index’ orientation

code_dict = {'d_one': [8, 7, 6, 0], 'd_two': ['a', 'b', 'c', 'd']}
pd.DataFrame.from_dict(code_dict, orient='index',
                       columns=['First', 'Second', 'Third', 'Fourth'])

Example 8: Create a dataframe with Pandas from_dict() Method

Pandas have Pandas.DataFrame.from_dict() method as well. If that sounds repetitive, because the standard constructor works with dictionaries, the from_dict() method supports dictionary-specific parameters, as shown in the example below.

The dictionary keys are represented as columns in the code. Row indexes are numerical. The default orientation is orient=’columns,’ which means to use the dictionary keys as columns and the values as rows.

import pandas as pd

code_dict =  {'sport': ["soccer", "basketball", "tennis", "atheletics"],
'terms': ["shoot", "3-pointer", "hit", "laps"]}
str_idx = ['Green', 'White', 'Blue','Red']

pd.DataFrame.from_dict(code_dict)

Now we turn it on its side. The rows will be the dictionary keys.

pd.DataFrame.from_dict(code_dict,orient='index')

There are no names in the columns, only numbers. Because that isn’t particularly useful, we’ll use the columns argument, which was added in Pandas 0.23. You are tasked with putting the column names in an array and sending it as the columns parameter. One wonders why earlier Panda versions lacked this feature.

pd.DataFrame.from_dict(code_dict,orient='index',columns=idx)

Conclusion

A Python dict (dictionary) that is a key-value pair can be used to build a Pandas DataFrame. We often construct a Pandas DataFrame in real-time by importing a CSV file or another resource. However, it is also feasible to generate it using a dict or a dictionary object.

Python Pandas are often used for data science/data processing and machine learning activities. It is built on the foundation of NumPy, another popular Python library for scientific computations. Pandas Dataframes are particularly useful in Python for working with 2D (two-dimensional) data. A Pandas DataFrame can be built in a variety of ways, one of which is by extracting data from a dictionary.

In this Pandas DataFrame from_dict() article, you learned how to convert a dictionary to a pandas dataframe using the from_dict() method as well as the dictionary’s list of values. You’ve also learned how to build a dataframe using dictionary keys and values as rows and columns. As well as with and without index columns. You can use this to convert the dictionary into a pandas dataframe of any size.

You may also like

Leave a Comment