Python dictionaries are used to store values as keys and values. You need to convert dictionaries to pandas dataframe to manipulate data for machine learning activities.
You can convert a dictionary to Pandas Dataframe using df = pd.DataFrame.from_dict(my_dict)
statement.
In this tutorial, you’ll learn the different methods available to convert python dict to Pandas dataframe.
If you’re in Hurry
You can use the following to convert the python dictionary to Pandas Dataframe.
import pandas as pd
my_dict = {"S.No.": [1], "Item":["CPU"], "Quantity": [3], "Price": [5000]}
df = pd.DataFrame.from_dict(my_dict)
df
Output
S.NO. | Item | Quantity | Price | |
---|---|---|---|---|
0 | 1 | CPU | 3 | 5000 |
If You Want to Understand Details, Read on…
You can use the from_dict()
method and you can orient the dictionary keys as columns or rows of the Pandas Dataframe. Let’s look into the detail.
Table of Contents
Convert Dictionary To Pandas Dataframe using from_Dict()
Pandas Dataframe provides the from_dict() method to create a Pandas Dataframe using a Dictionary object.
It accepts the following parameters.
Dictionary
– Dictionary with data to create dataframe. Mandatoryorient
– To specify the orientation. Whether dictionary must be rows or columns of a dataframe.dtype
– Datatype for the data in the pandas dataframe. Optional. The datatype of the data in the dictionary will be infered by default.columns
– list of name for columns, defaultNone
. Column labels to use whenorient='index'
. It raises aValueError
if used withorient='columns'
ororient='tight'
, becuase dictionary key will be used as column names in these cases.
You can use this method when the values of the dictionaries are in the list form. If not, you’ll face, If using all scalar values, you must pass an index error. You can refer to the next section if your dictionary key values are NOT in the list form.
Code
The following code demonstrates how to create a pandas dataframe using a dictionary.
- With only the mandatory parameter
dictionary
to create a pandas dataframe.
import pandas as pd
my_dict = {"S.No.": [1], "Item":["CPU"], "Quantity": [3], "Price": [5000]}
df = pd.DataFrame.from_dict(my_dict)
df
A pandas dataframe is created using the values in the dictionary.
S.NO. | Item | Quantity | Price | |
---|---|---|---|---|
0 | 1 | CPU | 3 | 5000 |
Convert Dictionary To Dataframe Using List of Items
You can convert the dictionary to a pandas dataframe by creating a list of Dictionary items using the list(my_dict.items())
. Also, you can pass the column header values using the columns
parameter.
Use this method when the values of the Dictionary keys are not a list of values.
Code
import pandas as pd
my_dict = {"S.No.": 1, "Item":"CPU", "Quantity": 3, "Price": 5000}
df = pd.DataFrame(list(my_dict.items()), columns = ['Name','Value'])
df
Output
Name | value | |
---|---|---|
0 | S.No. | 1 |
1 | Item | CPU |
2 | Quantity | 3 |
3 | Price | 5000 |
Convert Dictionary To Dataframe With Keys As Rows
In this section, you’ll learn how to convert a dictionary to pandas dataframe with Dictionary keys as rows in the pandas dataframe.
- You can do this by using the
orient = 'index'
parameter in thefrom_dict()
method as demonstrated below.
Code
import pandas as pd
my_dict = {"S.No.": [1], "Item":["CPU"], "Quantity": [3], "Price": [5000]}
df = pd.DataFrame.from_dict(my_dict, orient = 'index')
df
The dataframe is created with the dictionary keys as rows as shown below.
Output
0 | |
---|---|
S.NO. | 1 |
Item | CPU |
Quantity | 3 |
Price | 5000 |
Convert Dictionary To Dataframe With Keys As Columns
In this section, you’ll learn how to convert a dictionary to a pandas dataframe with Dictionary keys as columns in the pandas dataframe.
- You can do this by using the
orient = 'columns'
parameter in thefrom_dict()
method, as demonstrated below.
This is the default behavior of the from_dict()
method.
Code
import pandas as pd
my_dict = {"S.No.": [1], "Item":["CPU"], "Quantity": [3], "Price": [5000]}
df = pd.DataFrame.from_dict(my_dict, orient = 'columns')
df
The dataframe is created with the dictionary keys as rows as shown below.
Output
S.NO. | Item | Quantity | Price | |
---|---|---|---|---|
0 | 1 | CPU | 3 | 5000 |
Convert Dictionary To Dataframe With Index
In this section, you’ll learn how to convert dictionary to pandas dataframe with Index column for the dataframe.
The index can be set by using the set_index()
method.
The set_index() method accepts the following parameters.
keys
– label to set as the index(For each row).drop
– bool, defaultTrue
. Delete columns from the dataframe after using it asindex
column.append
– bool, defaultFalse
. Denotes whether to append columns to existing index.inplace
– bool, defaultFalse
. IfTrue
, modifies the DataFrame in place (do not create a new object).verify_integrity
– bool, defaultFalse
. Check the new index for duplicates. Otherwise, defer the check until necessary. Setting toFalse
will improve the performance of this method. If this isTrue
, an error will be thrown when the index keys contain duplicate values.
Code
The following code demonstrates how to convert a dictionary into a dataframe with an index column.
import pandas as pd
my_dict = {"S.No.": [1], "Item":["CPU"], "Quantity": [3], "Price": [5000]}
df = pd.DataFrame(my_dict)
df = df.set_index('S.NO.')
df
The pandas dataframe is created and the column S.No.
is used as index
column for the dataframe as shown below.
Output
Item | Quantity | Price | |
---|---|---|---|
S.NO. | |||
1 | CPU | 3 | 5000 |
Convert Dictionary To Dataframe Without Index
In this section, you’ll learn how to convert a dictionary to Pandas Dataframe without an index column.
- You can directly use the dictionary object to convert the dictionary to a dataframe without an index.
Code
import pandas as pd
my_dict = {"S.No.": [1], "Item":["CPU"], "Quantity": [3], "Price": [5000]}
df = pd.DataFrame(my_dict)
df
S.No. | Item | Quantity | Price | |
---|---|---|---|---|
0 | 1 | CPU | 3 | 5000 |
I am writing a comment simply because I LOVED the fact you put the portion in there ‘In Case You Are In A Hurry’. Of course I am and thank you for putting the answer to the main question first instead of some other bullshit so I have to sift through some lecture.
Thanks, Grover, for taking to time to appreciate even though ‘You’re in a hurry’ 😉
At stackvidhya, reader’s experience is important than any other metrics like “time spent on a page”.
We will keep writing more such content.
Regards,
Vikram