How to Create DataFrame from Dictionary in Python? Examples


Create Pandas DataFrame from Python Dictionary

You can create a DataFrame from Dictionary by passing a dictionary as the data argument to DataFrame() class.

In this tutorial, we shall learn how to create a Pandas DataFrame from Python Dictionary.


Syntax to create DataFrame

The syntax to create a DataFrame from dictionary object is shown below.

mydataframe = DataFrame(dictionary)

Each element in the dictionary is translated to a column, with the key as column name and the array of values as column values.

Python Pandas - Create DataFrame from Dictionary

Examples

1. Create DataFrame from Dictionary

In the following example, we will create a dictionary, and pass this dictionary as data argument to the DataFrame() class.

Python Program

import numpy as np
import pandas as pd

mydictionary = {'names': ['Somu', 'Kiku', 'Amol', 'Lini'],
	'physics': [68, 74, 77, 78],
	'chemistry': [84, 56, 73, 69],
	'algebra': [78, 88, 82, 87]}

#create dataframe using dictionary
df_marks = pd.DataFrame(mydictionary)

print(df_marks)

Explanation

  1. The program imports the numpy and pandas libraries, essential for numerical and tabular data operations.
  2. A dictionary named mydictionary is created with keys: 'names', 'physics', 'chemistry', and 'algebra'. Each key contains a list of values representing students' names and their respective subject marks.
  3. The pd.DataFrame(mydictionary) function converts the dictionary into a pandas DataFrame, with keys becoming column names and their corresponding lists forming the column data.
  4. The resulting DataFrame, stored in the variable df_marks, organizes the data in a tabular format with rows representing individual students and columns representing subjects and names.
  5. print(df_marks) outputs the DataFrame to the console, displaying student names alongside their marks in physics, chemistry, and algebra.

Output

  names  physics  chemistry  algebra
0   Geo       68         84       78
1  Kiku       74         56       88
2  Amol       77         73       82
3  Lini       78         69       87

The key values (names, physics, chemistry, algebra) transformed to column names and the array of values to column values.


2. Create DataFrame from Python Dictionary

In this example, we will create a DataFrame with two columns and four rows of data using a Dictionary.

Python Program

import numpy as np
import pandas as pd

mydictionary = {'names': ['Somu', 'Kiku', 'Amol', 'Lini'],
	'roll_no': [1, 2, 3, 4]
	}

#create dataframe using dictionary
df_students = pd.DataFrame(mydictionary)

print(df_students)

Explanation

  1. The program imports the required libraries numpy and pandas, which are commonly used for data manipulation and analysis.
  2. A dictionary named mydictionary is created, containing two keys: 'names' with a list of student names and 'roll_no' with corresponding roll numbers.
  3. pd.DataFrame(mydictionary) converts the dictionary into a pandas DataFrame, where the keys become column names and the values become the column data.
  4. The resulting DataFrame is assigned to the variable df_students.
  5. print(df_students) displays the DataFrame in a tabular format, showing the student names and roll numbers as rows and columns.

Output

  names  roll_no
0  Somu        1
1  Kiku        2
2  Amol        3
3  Lini        4

Video

https://youtu.be/vEq8qGrCrNo
Pandas Create DataFrame from Python Dictionary

Summary

In this Pandas Tutorial, we learned how to create a Pandas DataFrame from Python Dictionary with the help of well detailed examples.




Python Libraries