top of page
  • Writer's pictureEkta Aggarwal

From dictionary to DataFrame in Python

We can convert a dictionary to a dataframe using pandas' DataFrame function.

Let us firstly import pandas

import pandas as pd

Let us create a dictionary of key-values:

data1 = {
'Product_category': ['Makeup', 'Biscuits', 'Household_Cleaning', 'Cold_drinks'],
'Sales': [10000, 20000, 17891, 12340],
 'Returns' : [678, 2839, 678,234]}

Keys: Product_category, Sales and Returns

We can convert a dictionary like this to a dataframe using pandas' DataFrame( ) function where:

Keys will become the column names

Values will become entries for their corresponding keys (column)

data1 = pd.DataFrame(data1)

A list containing dictionaries!

We can also create a list where each row is a dictionary.

In the following list we have 4 dictionaries (i.e. our data will have 4 rows).

Each key-value pair denotes the column and its respective value.

data1 = [
    {'Product_category': 'Makeup', 'Sales': 10000,'Returns' : 678},
    {'Product_category': 'Biscuits', 'Sales': 20000,'Returns' : 2839},
    {'Product_category': 'Household_Cleaning', 'Sales': 17891,'Returns' : 678},
    {'Product_category': 'Cold_drinks', 'Sales': 12340,'Returns' : 234}
data1 = pd.DataFrame(data1)


bottom of page