DataFrames are 2-dimensional data structure in pandas. DataFrames consists of rows, columns and the data. DataFrame can be created with the help of python dictionaries or lists but here we are going to learn how to create a list of dictionaries from pandas DataFrame.
For this purpose, pandas provide us pandas.DataFrame.to_dict() method, which will allow us to achieve this task.
pandas.DataFrame.to_dict() Method
This method is used to convert a DataFrame into list of dictionaries which will looks like a JSON.
Syntax:
DataFrame.to_dict(
orient='dict',
into=<class 'dict'>
)
Parameter(s): It takes few parameters like dict, list, series, split, records, index which are passed inside the method.
To work with pandas, we need to import pandas package first, below is the syntax:
import pandas as pd
Let us understand with the help of an example.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer