Skip to content
Related Articles

Related Articles

Create a Pandas DataFrame from List of Dicts

View Discussion
Improve Article
Save Article
  • Last Updated : 16 Aug, 2022
View Discussion
Improve Article
Save Article

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.

Method 1: Convert a list of dictionaries to a pandas DataFrame using from_records

Pandas the from records() function of DataFrame. It 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
0  dataframe  using  list
1         10     20    30

Method 2: Convert a list of dictionaries to a pandas DataFrame using pd.DataFrame.from_dict

The DataFrame.from dict() method in Pandas. It 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

Method 3: 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', '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

Method 4: 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 resulted 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:

 

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', '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: 

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!