How to use pandas DataFrame In Python

By using pandas dataframe we will convert list of dict to dict of list

Syntax: pandas.DataFrame(list_of_dictionary).to_dict(orient=”list”)

Where:

  • list_of_dictionary is the input
  • to_dict() method is to convert into dictionary
  • orient parameter is to convert into list

Example:

Python3




#import pandas
import pandas as pd
 
# consider the list of dictionary
data = [{'manoj': 'java', 'bobby': 'python'},
        {'manoj': 'php', 'bobby': 'java'},
        {'manoj': 'cloud', 'bobby': 'big-data'}]
 
# convert into dictionary of list
# with list as values using pandas dataframe
pd.DataFrame(data).to_dict(orient="list")


Output:

{‘bobby’: [‘python’, ‘java’, ‘big-data’], ‘manoj’: [‘java’, ‘php’, ‘cloud’]}

Time Complexity: O(n), where n is the length of the given list of dictionary
Auxiliary Space: O(n)

Python – Convert list of dictionaries to dictionary of lists

In this article, we will discuss how to convert a list of dictionaries to a dictionary of lists.

Similar Reads

Method 1: Using for loop

By iterating based on the first key we can convert list of dict to dict of list. Python program to create student list of dictionaries...

Method 2: Using dictionary comprehension

...

Method 3: Using pandas DataFrame

...

Method 4: Using NumPy

Here we are using a dictionary comprehension to convert a list of dictionaries into the dictionary of the list, so we are passing the list as values based on keys in the new dictionary...

Contact Us