- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain how a dataframe structure can be created 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.
- Related Articles
- Explain how series data structure in Python can be created using dictionary and explicit index values?
- How can a dataframe be created using a dictionary of Series in Python?
- Explain how series data structure in Python can be created using scalar/constant values?
- How can a new column be created to a dataframe using the already present columns in Python?
- Python - Create a dictionary using list with none values
- Python - Convert list of nested dictionary into Pandas Dataframe
- Python – Inverse Dictionary Values List
- How can a column of a dataframe be deleted in Python?
- Combining values from dictionary of list in Python
- How can bubble charts be created using Matplotlib?
- How to get a list of all the values from a Python dictionary?
- How can series be created using Numpy and passing index value explicitly in Python?
- How to create Python dictionary from list of keys and values?
- Sort Dictionary key and values List in Python
- How to create a pandas DataFrame using a dictionary?
