Horje
Python Dictionary with Multiple Values to DataFrame

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




Reffered: https://www.geeksforgeeks.org


Android

Related
5 Best IPTV Service for Android Box 5 Best IPTV Service for Android Box
How to Use Cloud SQL to Create and Manage Relational Databases How to Use Cloud SQL to Create and Manage Relational Databases
Dielectric Heating - Definition, Working and Applications Dielectric Heating - Definition, Working and Applications
Arrays for Competitive Programming Arrays for Competitive Programming
Program to convert minutes to seconds Program to convert minutes to seconds

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
16