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.