python

Convert a Python Dictionary to Pandas DataFrame

Pandas, as an awesome Python data analysis library, is great at manipulating and fitting data. In this post, we will learn to create DataFrame from a dictionary with multiple methods.

import pandas as pd

# create a dictionary
user_info = {
  'name': ['Rick', 'John', 'Callumn', 'Manoj', 'Dustin'],
  'address': ['New York', 'LA', 'London', 'Delhi', 'Holland'],
  'designation': ['Programmer', 'Accountant', 'Admin', 'Tech Lead', 'Manager']
}

# create the dataframe from a dictionary
df = pd.DataFrame(user_info)

print(df)

Output

╒════╤═════════╤═══════════╤═══════════════╕
│    │ name    │ address   │ designation   │
╞════╪═════════╪═══════════╪═══════════════╡
│  0 │ Rick    │ New York  │ Programmer    │
├────┼─────────┼───────────┼───────────────┤
│  1 │ John    │ LA        │ Accountent    │
├────┼─────────┼───────────┼───────────────┤
│  2 │ Callumn │ London    │ Admin         │
├────┼─────────┼───────────┼───────────────┤
│  3 │ Manoj   │ Delhi     │ Tech Lead     │
├────┼─────────┼───────────┼───────────────┤
│  4 │ Dustin  │ Holland   │ Manager       │
╘════╧═════════╧═══════════╧═══════════════╛

Creating DataFrame from a Python Dictionary is very easy. You just need to pass the dictionary to Pandas.DataFrame() function and the dictionary will be converted to a Pandas Dataframe.

As we know Pandas is an awesome Python data analysis library, that is great at manipulating and fitting data. But if you have data in dictionary format then you can use the Pandas functions on it after converting it to a DataFrame.

The keys in the dictionary are considered as the column names for DataFrame and the values will be added as rows in the DataFrame.

Syntax

pd.DataFrame(Dict)

# Code example 2

Was this helpful?