The Wayback Machine - https://web.archive.org/web/20240617094124/https://www.geeksforgeeks.org/python-creating-dataframe-from-dict-of-narray-lists/
Open In App

Python | Creating DataFrame from dict of narray/lists

Last Updated : 25 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

As we know Pandas is all-time great tools for data analysis. One of the most important data type is dataframe. It 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. Let’s discuss how to create Pandas dataframe using dictionary of ndarray (or lists). Let’s try to understand it better with a few examples.

Using Pandas DataFrame to create Dataframe from List

Example #1:  We will use pd.DataFrame() function to create the dataframe from the list.

Python3




# Python code demonstrate creating
# DataFrame from dict narray / lists
# By default addresses.
 
import pandas as pd
 
# initialise data of lists.
data = {'Category':['Array', 'Stack', 'Queue'],
        'Marks':[20, 21, 19]}
 
# Create DataFrame
df = pd.DataFrame(data)
 
# Print the output.
print(df)


Output:

  Category  Marks
0    Array     20
1    Stack     21
2    Queue     19

Note: To create DataFrame from dict of narray/list, all the narray must be of same length. If index is passed then the length index should be equal to the length of arrays. If no index is passed, then by default, index will be range(n) where n is the array length.

Example #2:

Python3




# Python code demonstrate creating
# DataFrame from dict narray / lists
# By default addresses.
 
import pandas as pd
 
# initialise data of lists.
data = {'Category':['Array', 'Stack', 'Queue'],
        'Student_1':[20, 21, 19], 'Student_2':[15, 20, 14]}
 
# Create DataFrame
df = pd.DataFrame(data)
 
# Print the output.
print(df.transpose())


Output:

               0      1      2
Category   Array  Stack  Queue
Student_1     20     21     19
Student_2     15     20     14

Using Pandas Dataframe with the index parameter

Example #1: Providing index list to dataframe 

Python3




# Python code demonstrate creating
# DataFrame from dict narray / lists
# By default addresses.
 
import pandas as pd
 
# initialise data of lists.
data = {'Area':['Array', 'Stack', 'Queue'],
        'Student_1':[20, 21, 19], 'Student_2':[15, 20, 14]}
 
# Create DataFrame
df = pd.DataFrame(data, index =['Cat_1', 'Cat_2', 'Cat_3'])
 
# Print the output.
print(df)


Output:

        Area  Student_1  Student_2
Cat_1  Array         20         15
Cat_2  Stack         21         20
Cat_3  Queue         19         14


Previous Article
Next Article

Similar Reads

Difference between dict.items() and dict.iteritems() in Python
dict.items() and dict.iteriteams() almost does the same thing, but there is a slight difference between them - dict.items(): returns a copy of the dictionary’s list in the form of (key, value) tuple pairs, which is a (Python v3.x) version, and exists in (Python v2.x) version.dict.iteritems(): returns an iterator of the dictionary’s list in the form
3 min read
Python Convert Dict of Lists to Dict of Sets
In Python, converting a dictionary of lists to a dictionary of sets is a valuable process for optimizing data structures. This transformation enhances efficiency by eliminating duplicates and enabling efficient membership testing. Utilizing built-in functions like set() and list comprehension simplifies the conversion process. Example : Input: dict
3 min read
Choosing dict.get() Over dict[] in Python
In Python, dictionaries are a fundamental data structure used to store key-value pairs. Accessing values in a dictionary using a key can be done in two primary ways: using the square bracket notation (dict[key]) or the get method (dict.get(key)). While both methods retrieve the value associated with the specified key, there are key differences betw
2 min read
How to use dict.get() with multidimensional dict?
The get() method in python returns the value for the key if the key is in the dictionary. If the key is not present in a dictionary it returns None. It also takes another optional parameter which is the value that will be returned if the key is not found in a dictionary. Syntax: dict.get(key, value) Here, the key is a must parameter which is a key
5 min read
Creating a Pandas DataFrame using Dict of Tuples in Python
Working with Python dictionaries containing tuples is a common scenario, especially when dealing with heterogeneous data. Transforming this data into a Pandas DataFrame allows for efficient manipulation and analysis. In this article, we will see how we can convert a dictionary with a tuple into a DataFrame in Python. Convert Dictionary with Tuple i
2 min read
Python | Create a Pandas Dataframe from a dict of equal length lists
Given a dictionary of equal length lists, task is to create a Pandas DataFrame from it. There are various ways of creating a DataFrame in Pandas. One way is to convert a dictionary containing lists of equal lengths as values. Let's discuss how to create a Pandas Dataframe from a dict of equal length lists with help of examples. Example #1: Given a
2 min read
Python - Convert dict of list to Pandas dataframe
In this article, we will discuss how to convert a dictionary of lists to a pandas dataframe. Method 1: Using DataFrame.from_dict() We will use the from_dict method. This method will construct DataFrame from dict of array-like or dicts. Syntax: pandas.DataFrame.from_dict(dictionary) where dictionary is the input dictionary Example: Program to take s
2 min read
Python | Convert a list of lists into tree-like dict
Given a list of lists, write a Python program to convert the given list of lists into a tree-like dictionary. Examples: Input : [[1], [2, 1], [3, 1], [4, 2, 1], [5, 2, 1], [6, 3, 1], [7, 3, 1]] Output : {1: {2: {4: {}, 5: {}}, 3: {6: {}, 7: {}}}} Input : [['A'], ['B', 'A'], ['C', 'A'], ['D', 'C', 'A']] Output : {'A': {'C': {'D': {}}, 'B': {}}} Meth
4 min read
enchant.Dict() in Python
Enchant is a module in python which is used to check the spelling of a word, gives suggestions to correct words. Also, gives antonym and synonym of words. It checks whether a word exists in dictionary or not. enchant.Dict() enchant.Dict() is an inbuilt method of enchant module. It is used to create a Dict object, which is the most important object
2 min read
Python program to convert Dict of list to CSV
Given a dictionary of list d, the task is to write a Python program to write a dictionary into a CSV file. The idea to solve this problem is to zip the dictionary and then write it into a CSV file. Python provides an in-built module called ‘csv’ for working with CSV files. csv.writer class is used to write into CSV file. csv.writer class provide tw
2 min read