Python Dictionary with Multiple Values to DataFrame
Last Updated :
28 Apr, 2025
Python's powerful libraries like Pandas make data manipulation and analysis easier. One of the most common things that can be performed with Pandas is to convert data structures like Dictionaries into Dataframes. In this article, we are covering how to convert Python dictionaries with multiple values into DataFrames.
Convert Python Dictionary with Multiple Values to DataFrame
Below are some ways by which we can convert a Python Dictionary with Multiple Values to a DataFrame in Python:
- Using pd.DataFrame()
- Using pd.DataFrame.from_dict()
- Using pd.DataFrame.from_records()
- Using pd.DataFrame.from_dict with orient
Convert Dictionary to DataFrame Using pd.DataFrame()
In this example, a Pandas DataFrame, `df`, is directly created from a nested dictionary using pd.DataFrame(data), where each outer key represents a column, inner keys serve as row indices, and corresponding values fill the table. The resulting DataFrame displays tabular data with students as rows and attributes as columns.
Python3
import pandas as pd
data = {
"Shravan": {"Enrollment No.": 225, "Branch": "CSE", "CGPA": 7.5},
"Jitu": {"Enrollment No.": 250, "Branch": "CSE", "CGPA": 7.0},
"Ram": {"Enrollment No.": 249, "Branch": "CSE", "CGPA": 9.1},
}
# Using pd.DataFrame directly
df = pd.DataFrame(data)
# Print the DataFrame
print(df)
Output:
Shravan Jitu Ram
Enrollment No. 225 250 249
Branch CSE CSE CSE
CGPA 7.5 7.0 9.1
Python Convert Dictionary to DataFrame Using pd.DataFrame.from_dict()
In this example, a Pandas DataFrame is created from a nested dictionary using pd.DataFrame.from_dict() where each outer key represents a student's name and inner keys correspond to enrollment number, branch, and CGPA. The resulting DataFrame, `df`, displays this tabular data with students as rows and attributes as columns.
Python3
import pandas as pd
data = {
"Shravan": {"Enrollment No.": 225, "Branch": "CSE", "CGPA": 7.5},
"Jitu": {"Enrollment No.": 250, "Branch": "CSE", "CGPA": 7.0},
"Ram": {"Enrollment No.": 249, "Branch": "CSE", "CGPA": 9.1},
}
df = pd.DataFrame.from_dict(data)
print(df)
Output:
Shravan Jitu Ram
Enrollment No. 225 250 249
Branch CSE CSE CSE
CGPA 7.5 7.0 9.1
Python Dictionary to Pandas DataFrame Using pd.DataFrame.from_records()
In this example, a Pandas DataFrame, `df_method3`, is created using pd.DataFrame.from_records() with a list of key-value pairs extracted from the nested dictionary. The resulting DataFrame displays tabular data with students as rows and attributes as columns.
Python3
import pandas as pd
data = {
"Shravan": {"Enrollment No.": 225, "Branch": "CSE", "CGPA": 7.5},
"Jitu": {"Enrollment No.": 250, "Branch": "CSE", "CGPA": 7.0},
"Ram": {"Enrollment No.": 249, "Branch": "CSE", "CGPA": 9.1},
}
# Using pd.DataFrame.from_records
df_method3 = pd.DataFrame.from_records(list(data.items()))
# Print the DataFrame
print(df_method3)
Output:
0 1
0 Shravan {'Enrollment No.': 225, 'Branch': 'CSE', 'CGPA...
1 Jitu {'Enrollment No.': 250, 'Branch': 'CSE', 'CGPA...
2 Ram {'Enrollment No.': 249, 'Branch': 'CSE', 'CGPA...
Python Dict into DataFrame Using orient Parameter
In this example, two Pandas DataFrames are created from the nested dictionary 'data' using the pd.DataFrame.from_dict()
method with different orientation settings
Python3
import pandas as pd
data = {
"Shravan": {"Enrollment No.": 225, "Branch": "CSE", "CGPA": 7.5},
"Jitu": {"Enrollment No.": 250, "Branch": "CSE", "CGPA": 7.0},
"Ram": {"Enrollment No.": 249, "Branch": "CSE", "CGPA": 9.1},
}
print("DataFrame with columns as index and rows as values (orient='index')")
df_index = pd.DataFrame.from_dict(data, orient='index')
print(df_index)
print("\nDataFrame with values as columns and names as rows (orient='columns')")
df_columns = pd.DataFrame.from_dict(data, orient='columns')
print(df_columns)
Output:
DataFrame with columns as index and rows as values (orient='index')
Enrollment No. Branch CGPA
Shravan 225 CSE 7.5
Jitu 250 CSE 7.0
Ram 249 CSE 9.1
DataFrame with values as columns and names as rows (orient='columns')
Shravan Jitu Ram
Enrollment No. 225 250 249
Branch CSE CSE CSE
CGPA 7.5 7.0 9.1
Similar Reads
Create Pandas Dataframe Dictionary With Tuple As Key In Python, Pandas is a powerful data manipulation library, and a dataframe is a data structure. In this article, we will explore how to create a Pandas Dataframe Dictionary with Tuple as a Key. What is a data frame?Dataframes are a fundamental data structure. A data frame is a two-dimensional data s
2 min read
Make a Pandas DataFrame with two-dimensional list | Python In this discussion, we will illustrate the process of creating a Pandas DataFrame with the two-dimensional list. Python is widely recognized for its effectiveness in data analysis, thanks to its robust ecosystem of data-centric packages. Among these packages, Pandas stands out, streamlining the impo
3 min read
Python | Pandas DataFrame.values Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. It can be thought of as a dict-like container for Series objects. This is the primary data structure o
2 min read
Python | Pandas dataframe.set_value() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.set_value() function put a single value at passed column and index. I
2 min read
How to create DataFrame from dictionary in Python-Pandas? The task of converting a dictionary into a Pandas DataFrame involves transforming a dictionary into a structured, tabular format where keys represent column names or row indexes and values represent the corresponding data.Using Default ConstructorThis is the simplest method where a dictionary is dir
3 min read
How to write Pandas DataFrame as TSV using Python? In this article, we will discuss how to write pandas dataframe as TSV using Python. Let's start by creating a data frame. It can be done by importing an existing file, but for simplicity, we will create our own. Python3 # importing the module import pandas as pd # creating some sample data sample =
1 min read
Split dataframe in Pandas based on values in multiple columns In this article, we are going to see how to divide a dataframe by various methods and based on various parameters using Python. To divide a dataframe into two or more separate dataframes based on the values present in the column we first create a data frame. Creating a DataFrame for demonestrationPy
3 min read
Read multiple CSV files into separate DataFrames in Python Sometimes you might need to read multiple CSV files into separate Pandas DataFrames. Importing CSV files into DataFrames helps you work on the data using Python functionalities for data analysis. In this article, we will see how to read multiple CSV files into separate DataFrames. For reading only o
2 min read
Manipulating DataFrames with Pandas - Python Before manipulating the dataframe with pandas we have to understand what is data manipulation. The data in the real world is very unpleasant & unordered so by performing certain operations we can make data understandable based on one's requirements, this process of converting unordered data into
4 min read
Add multiple columns to dataframe in Pandas In Pandas, we have the freedom to add columns in the data frame whenever needed. There are multiple ways to add columns to pandas dataframe. Add multiple columns to a DataFrame using ListsPython3 # importing pandas library import pandas as pd # creating and initializing a nested list students = [['
3 min read