Friday, December 27, 2024
Google search engine
HomeLanguagesCreate a Pandas DataFrame from List of Dicts

Create a Pandas DataFrame from List of Dicts

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.

Convert a list of dictionaries to a pandas DataFrame 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', 'Lazyroar': 'list'},
        {'Geeks':10, 'For': 20, 'Lazyroar': 30}]
 
df = pd.DataFrame.from_records(data,index=['1', '2'])
print(df)


Output:

       Geeks    For Lazyroar
0 dataframe using list
1 10 20 30

Convert a 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', 'Lazyroar': 'list'},
        {'Geeks':10, 'For': 20, 'Lazyroar': 30}]
 
df = pd.DataFrame.from_dict(data)
print(df)


Output:

       Geeks    For Lazyroar
0 dataframe using list
1 10 20 30

Convert a list of dictionaries to a pandas DataFrame 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', 'Lazyroar': 'list'},
        {'Geeks':10, 'For': 20, 'Lazyroar': 30}]
 
df=pd.json_normalize(data)
print(df)


Output:

       Geeks    For Lazyroar
0 dataframe using list
1 10 20 30

Convert a 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', 'Lazyroar': 'list', 'Portal': 10000},
        {'Geeks':10, 'For': 20, 'Lazyroar': 30}]
   
# Creates DataFrame.
df = pd.DataFrame(data)
   
# Print the data
df


Output:

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

We can conclude from the above example that if there is no matching key-value pair in the list of dictionaries then the NaN value will be inserted in that place.

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', 'Lazyroar': 'list'},
        {'Geeks': 10, 'For': 20, 'Lazyroar': 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 Lazyroar
indx dataframe using list
indy 10 20 30

RELATED ARTICLES

Most Popular

Recent Comments