How to convert Python dictionary to Pandas DataFrame

  1. Method to convert dictionary to Pandas DataFame
  2. Method to convert keys to be the columns and the values to be the row values in Pandas dataframe
  3. pandas.DataFrame().from_dict() method to convert dict into dataframe

We will introduce the method to convert the python dictionary to Pandas datafarme, and options like having keys to be the columns and the values to be the row values and convert the nested dictionary to dataframe.

We will also introduce another approach using pandas.DataFrame.from_dict, we will chain this with any rename method also to set the names of the index and columns in one go.

Method to convert dictionary to Pandas DataFame

Pandas DataFrame constructor pd.DataFrame() converts the dictionary to dataframe if the items of the dictionary is given as the argument of the constructor, but not the dictionary itself.

# python 3.x
import pandas as pd

fruit_dict = {
    3: 'apple',
    2: 'banana',
    6:'mango',
    4:'apricot',
    1:'kiwi',
    8:'orange'}

print(pd.DataFrame(list(fruit_dict.items()),
                   columns=['Quantity', 'FruitName']))

The keys and values of the dictionary are converted to two columns of the dataframe with the column names as given in the options columns.

   Quantity FruitName
0         3     apple
1         2    banana
2         6     mango
3         4   apricot
4         1      kiwi
5         8    orange

Method to convert keys to be the columns and the values to be the row values in Pandas dataframe

We can simply put brackets around the dictionary and remove the column name from above code like this:

import pandas as pd

fruit_dict = {
    1: 'apple',
    2: 'banana',
    3:'mango',
    4:'apricot',
    5:'kiwi',
    6:'orange'}

print(pd.DataFrame([fruit_dict]))

Output:

       1       2      3        4     5       6
0  apple  banana  mango  apricot  kiwi  orange

We will use pandas dictionary comprehension with concat to combined the all dictionaries and then pass the list to give new columns name

Consider the following code,

import pandas as pd

data = {
    '1':{
        'apple':11, 
        'banana':18}, 
    '2':{
        'apple':16, 
        'banana':12}
}
df = pd.concat({k: pd.Series(v) for k, v in data.items()}).reset_index()
df.columns = ['dict_index', 'name','quantity']
print(df)

Output:

  dict_index    name  quantity
0          1   apple        11
1          1  banana        18
2          2   apple        16
3          2  banana        12

pandas.DataFrame().from_dict() method to convert dict into dataframe

We will use from_dict to convert dict into dataframe, here we set orient='index' for using dictionary keys as rows and applying raname() method to change the column name.

Consider the following code,

import pandas as pd

print(pd.DataFrame.from_dict({
 'apple': 3,
 'banana': 5,
 'mango': 7,
 'apricot': 1,
 'kiwi': 8,
 'orange': 3}, orient='index').rename(columns={0:'Qunatity'}))

Output:

         Quantity
apple           3
banana          5
mango           7
apricot         1
kiwi            8
orange          3

Related Article - Pandas DataFrame

  • How to Get Pandas DataFrame Column Headers as a List
  • How to Delete Pandas DataFrame Column
  • How to Convert DataFrame Column to Datetime in Pandas
  • How to Convert a float to an integer in Pandas DataFrame
  • How to Sort Pandas DataFrame by One Column's Values
  • How to get the aggregate of Pandas group-by and Sum
  • How to add header row to a pandas DataFrame
  • How to convert Pandas Dataframe to Numpy array
  • How to convert index of a Pandas Dataframe into a column
  • How to count the NaN occurrences in a column in Pandas Dataframe
  • How to change the order of Pandas DataFrame columns
  • How to add one row to Pandas DataFrame
  • How to delete a row based on column value in Pandas DataFrame
  • How to get a value from a cell of a Pandas DataFrame
  • comments powered by Disqus