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
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
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
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
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
import pandas as pd
data = [{ 'Geeks' : 'dataframe' , 'For' : 'using' , 'geeks' : 'list' , 'Portal' : 10000 },
{ 'Geeks' : 10 , 'For' : 20 , 'geeks' : 30 }]
df = pd.DataFrame(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
data = [{ 'Geeks' : 'dataframe' , 'For' : 'using' , 'geeks' : 'list' },
{ 'Geeks' : 10 , 'For' : 20 , 'geeks' : 30 }]
df1 = pd.DataFrame(data, index = [ 'ind1' , 'ind2' ],
columns = [ 'Geeks' , 'For' ])
df2 = pd.DataFrame(data, index = [ 'indx' , 'indy' ])
print (df1, "\n" )
print (df2)
|
Output
Geeks For
ind1 dataframe using
ind2 10 20
Geeks For geeks
indx dataframe using list
indy 10 20 30
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Dec, 2023
Like Article
Save Article