
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create DataFrame Structure Using List of Dictionary Values in Python
Dataframe is a two dimensional data structure, where data is stored in a tabular format, in the form of rows and columns.
It can be visualized as an SQL data table or an excel sheet representation.
It can be created using the following constructor −
pd.Dataframe(data, index, columns, dtype, copy)
The ‘data’, ‘index’, ‘columns’, ‘dtype’ and ‘copy’ are not compulsory values.
A list of dictionaries can be passed as input to the dataframe. The keys of dictionary are taken as column names by default. Let us see an example −
Example
import pandas as pd my_data = [{'ab' : 34}, {'mn' : 56},{ 'gh' : 78}, {'wq' : 90},{'az' : 123},{'kl' : 45}] my_df = pd.DataFrame(my_data) print("The dataframe created from list of dictionary : ") print(my_df)
Output
The dataframe created from list of dictionary : ab az gh kl mn wq 0 34.0 NaN NaN NaN NaN NaN 1 NaN NaN NaN NaN 56.0 NaN 2 NaN NaN 78.0 NaN NaN NaN 3 NaN NaN NaN NaN NaN 90.0 4 NaN 123.0 NaN NaN NaN NaN 5 NaN NaN NaN 45.0 NaN NaN
Explanation
The required libraries are imported, and given alias names for ease of use.
A list of dictionary values is created, wherein a key-value pair is present in one dictionary.
In this way, multiple dictionaries are created and stored in a list.
This list of dictionary is later passed as a parameter to the ‘Dataframe’ function present in the ‘pandas’ library
The dataframe is created by passing the list of dictionary values as parameters to it.
The dataframe is printed on the console.
Note − The word ‘NaN’ refers to ‘Not a Number’, which means that specific [row,col] value doesn’t have any valid entry.