How about we examine how to change Python Dictionary over to Pandas Dataframe. We can change a word reference over to a pandas dataframe by utilizing the pd.DataFrame.from_dict() class-technique.
Use dict.items() to get a set-like article with the keys and upsides of dict. Utilize list(iterable) with iterable as the set-like item to change it over to a rundown. To make a DataFrame, use pandas.DataFrame(data) with this rundown as information.
data_dict = {"a": 1, "b": 2, "c": 3}
data_items = data_dict.items()
data_list = list(data_items)
df = pd.DataFrame(data_list)
create DataFrame from `data_list`
print(df)
0 1
0 a 1
1 b 2
2 c 3
The keys are Unicode dates and the qualities are numbers. I might want to change over this into a pandas dataframe by having the dates and their comparing esteems as two separate segments. Model: col1: Dates col2: DateValue (the dates are still Unicode and datevalues are still whole numbers)
Any assistance toward this path would be greatly valued. I can’t track down assets on the pandas docs to assist me with this.
I realize one arrangement may be to change over each key-esteem pair in this dict, into a dict so the whole construction turns into a dict of dicts, and afterward we can add each column independently to the dataframe. However, I need to know whether there is a more straightforward way and a more straightforward method for doing this.
Table of Contents
Steps to Convert a Dictionary to Pandas DataFrame
Step 1: Gather the Data for the Dictionary
To start, gather the data for your dictionary.
For example, let’s gather the following data about products and prices:
Product | Price |
Computer | 1500 |
Monitor | 300 |
Printer | 150 |
Desk | 250 |
Step 2: Create the Dictionary
Next, create the dictionary.
For our example, you may use the following code to create the dictionary:
my_dict = {'Computer':1500,'Monitor':300,'Printer':150,'Desk':250} print (my_dict) print (type(my_dict))
Run the code in Python, and you’ll get the following dictionary:
{'Computer': 1500, 'Monitor': 300, 'Printer': 150, 'Desk': 250}
<class 'dict'>
Notice that the syntax of print (type(my_dict)) was add at the bottom of the code to confirm that we indeed got a dictionary.
Step 3: Convert the Dictionary to a DataFrame
For the final step, convert the dictionary to a DataFrame using this template:
import pandas as pd my_dict = {key:value,key:value,key:value,...} df = pd.DataFrame(list(my_dict.items()),columns = ['column1','column2'])
For our example, here is the complete Python code to convert the dictionary to a DataFrame:
import pandas as pd my_dict = {'Computer':1500,'Monitor':300,'Printer':150,'Desk':250} df = pd.DataFrame(list(my_dict.items()),columns = ['Products','Prices']) print (df) print (type(df))
As you can see, the dictionary got converted to Pandas DataFrame:
Products Prices
0 Computer 1500
1 Monitor 300
2 Printer 150
3 Desk 250
<class 'pandas.core.frame.DataFrame'>
Note that the syntax of print (type(df)) was added at the bottom of the code to confirm that we actually got a DataFrame.