Open In App

Create a Pandas DataFrame from List of Dicts

Improve
Improve
Like Article
Like
Save
Share
Report

Pandas DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. It is generally the most commonly used Pandas object. Pandas DataFrame can be created in multiple ways using Python. Let’s discuss how to create a Pandas DataFrame from the List of Dictionaries.

Create a Pandas DataFrame from List of Dictionaries

Below are the ways by which we can create a Pandas DataFrame from list of dicts:

  • Using from_records()
  • Using pd.DataFrame.from_dict()
  • Using pd.json_normalize
  • Using pd.DataFrame

Create a Pandas DataFrame from List of Dictionaries Using from_records()

Pandas from_records() function of DataFrame changes structured data or records into DataFrames. It converts a structured ndarray, tuple or dict sequence, or DataFrame into a DataFrame object.

Python3




import pandas as pd 
    
# Initialise data to lists. 
data = [{'Geeks': 'dataframe', 'For': 'using', 'geeks': 'list'},
        {'Geeks':10, 'For': 20, 'geeks': 30}] 
  
df = pd.DataFrame.from_records(data,index=['1', '2'])
print(df)


Output

       Geeks    For geeks
1 dataframe using list
2 10 20 30

Convert List of Dictionaries to a Pandas DataFrame Using pd.DataFrame.from_dict()

The DataFrame.from dict() method in Pandas builds DataFrame from a dictionary of the dict or array type. By using the dictionary’s columns or indexes and allowing for Dtype declaration, it builds a DataFrame object.

Python3




import pandas as pd 
    
# Initialise data to lists. 
data = [{'Geeks': 'dataframe', 'For': 'using', 'geeks': 'list'},
        {'Geeks':10, 'For': 20, 'geeks': 30}] 
  
df = pd.DataFrame.from_dict(data)
print(df)


Output

       Geeks    For geeks
0 dataframe using list
1 10 20 30

Create a Pandas DataFrame from List of Dictionaries Using pd.json_normalize

Pandas have a nice inbuilt function called json_normalize() to flatten the simple to moderately semi-structured nested JSON structures to flat tables.

Python3




import pandas as pd 
    
# Initialise data to lists. 
data = [{'Geeks': 'dataframe', 'For': 'using', 'geeks': 'list'},
        {'Geeks':10, 'For': 20, 'geeks': 30}] 
  
df=pd.json_normalize(data)
print(df)


Output

       Geeks    For geeks
0 dataframe using list
1 10 20 30

Convert List of Dictionaries to a Pandas DataFrame Using pd.DataFrame

Example 1: As we know while creating a data frame from the dictionary, the keys will be the columns in the resulted Dataframe. When we create Dataframe from a list of dictionaries, matching keys will be the columns and corresponding values will be the rows of the Dataframe. If there are no matching values and columns in the dictionary, then the NaN value will be inserted into the resulting Dataframe.

Python3




# Python code demonstrate how to create  
# Pandas DataFrame by lists of dicts without matching key-value pair 
import pandas as pd 
    
# Initialise data to lists. 
data = [{'Geeks': 'dataframe', 'For': 'using', 'geeks': 'list', 'Portal': 10000},
        {'Geeks':10, 'For': 20, 'geeks': 30}] 
    
# Creates DataFrame. 
df = pd.DataFrame(data) 
    
# Print the data 
df


Output

       Geeks    For geeks   Portal
0 dataframe using list 10000.0
1 10 20 30 NaN

Example 2: Creating a Dataframe by explicitly providing user-defined values for both index and columns 

Python3




import pandas as pd
  
# Initialise data to lists.
data = [{'Geeks': 'dataframe', 'For': 'using', 'geeks': 'list'},
        {'Geeks': 10, 'For': 20, 'geeks': 30}]
  
# With two column indices, values same
# as dictionary keys
df1 = pd.DataFrame(data, index=['ind1', 'ind2'],
                   columns=['Geeks', 'For'])
  
# With two column indices with
# one index with other name
df2 = pd.DataFrame(data, index=['indx', 'indy'])
  
# print for first data frame
print(df1, "\n")
  
# Print for second DataFrame.
print(df2)


Output

          Geeks    For
ind1 dataframe using
ind2 10 20
Geeks For geeks
indx dataframe using list
indy 10 20 30


Last Updated : 26 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads