Create a Pandas DataFrame from List of Dicts
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 = [{ 'Beginner' : 'dataframe' , 'For' : 'using' , 'Beginner' : 'list' }, { 'Beginner' : 10 , 'For' : 20 , 'Beginner' : 30 }] df = pd.DataFrame.from_records(data,index = [ '1' , '2' ]) print (df) |
Output
Beginner For Beginner
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 = [{ 'Beginner' : 'dataframe' , 'For' : 'using' , 'Beginner' : 'list' }, { 'Beginner' : 10 , 'For' : 20 , 'Beginner' : 30 }] df = pd.DataFrame.from_dict(data) print (df) |
Output
Beginner For Beginner
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 = [{ 'Beginner' : 'dataframe' , 'For' : 'using' , 'Beginner' : 'list' }, { 'Beginner' : 10 , 'For' : 20 , 'Beginner' : 30 }] df = pd.json_normalize(data) print (df) |
Output
Beginner For Beginner
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 = [{ 'Beginner' : 'dataframe' , 'For' : 'using' , 'Beginner' : 'list' , 'Portal' : 10000 }, { 'Beginner' : 10 , 'For' : 20 , 'Beginner' : 30 }] # Creates DataFrame. df = pd.DataFrame(data) # Print the data df |
Output
Beginner For Beginner 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 = [{ 'Beginner' : 'dataframe' , 'For' : 'using' , 'Beginner' : 'list' }, { 'Beginner' : 10 , 'For' : 20 , 'Beginner' : 30 }] # With two column indices, values same # as dictionary keys df1 = pd.DataFrame(data, index = [ 'ind1' , 'ind2' ], columns = [ 'Beginner' , '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
Beginner For
ind1 dataframe using
ind2 10 20
Beginner For Beginner
indx dataframe using list
indy 10 20 30
Contact Us