Q:

Convert list of dictionaries to a pandas DataFrame

belongs to collection: Python Pandas Programs

0

DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and the data. In the real world, CSV files are imported and then converted into DataFrames, but DataFrame can be created with the help of python dictionaries, lists, or a list of dictionaries. Here, we are going to see how to convert a list of dictionaries into pandas DataFrame?

pandas.DataFrame() method

To work with pandas, we need to install the pandas package. Inside pandas, we have the DataFrame() method which is used to create a DataFrame. It takes a series/sequence or dictionaries as a parameter to convert it into a Datarame.

Syntax:

pandas.DataFrame(
    data=None, 
    index=None, 
    columns=None, 
    dtype=None, 
    copy=None
    )

To work with Python Pandas, we need to import the pandas library. Below is the syntax,

import pandas as pd

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Example:

# Importing pandas package
import pandas as pd

# Creating a list of dictionary
data=[{'Product':'Television','Stock':300,'Production':25,'Price':20000},
      {'Product':'Mobile','Stock':500,'Production':250,'Price':10000},
      {'Product':'Headphones','Stock':100,'Production':20,'Price':2000}]

# Converting a list of dictionary into DataFrame 
# by using pd.DataFrame method
# 'data' will be passed as a parameter inside 
# DataFrame() method
df = pd.DataFrame(data)

# Display the DataFrame
print("\nDataFrame created:\n\n",df)

Output:

DataFrame created:

    Price     Product  Production  Stock
0  20000  Television          25    300
1  10000      Mobile         250    500
2   2000  Headphones          20    100

Convert list of dictionaries to a pandas DataFrame | Output

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to pretty-print an entire Pandas DataFrame?... >>
<< Deleting DataFrame row in Pandas based on column v...