How to Create DataFrame from Dictionary in Python?

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)
Run Code Copy

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)
Run Code Copy

Output

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

Video

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.

Related Tutorials

Code copied to clipboard successfully 👍