Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Flatten Nested Dictionary to Matrix

Python – Flatten Nested Dictionary to Matrix

Sometimes, while working with data, we can have a problem in which we need to convert a nested dictionary into Matrix, each nesting comprising the different rows in the matrix. This can have applications in many data domains. Let us discuss certain ways in which this task can be performed.

Method #1 : Using loop + zip() + map()

The combination of the above functions can be used to perform this task. In this, we use brute force to flatten the dictionary keys and then use map() and zip() to align them as rows of a matrix.

Python3




# Python3 code to demonstrate working of
# Flatten Nested Dictionary to Matrix
# using zip() + loop + map()
 
# initializing dictionary
test_dict = {'Gfg1': {'CS': 1, 'GATE': 2},
             'Gfg2': {'CS': 2, 'GATE': 3},
             'Gfg3': {'CS': 4, 'GATE': 5}}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Flatten Nested Dictionary to Matrix
# using zip() + loop + map()
temp = list(test_dict.values())
sub = set()
for ele in temp:
    for idx in ele:
        sub.add(idx)
res = []
res.append(sub)
for key, val in test_dict.items():
    temp2 = []
    for idx in sub:
        temp2.append(val.get(idx, 0))
    res.append(temp2)
 
res = [[idx for idx, val in test_dict.items()]] + list(map(list, zip(*res)))
 
# printing result
print("The Grouped dictionary list is : " + str(res))


Output : 
The original dictionary is : {‘Gfg3’: {‘GATE’: 5, ‘CS’: 4}, ‘Gfg1’: {‘GATE’: 2, ‘CS’: 1}, ‘Gfg2’: {‘GATE’: 3, ‘CS’: 2}} 
The Grouped dictionary list is : [[‘Gfg3’, ‘Gfg1’, ‘Gfg2’], [‘GATE’, 5, 2, 3], [‘CS’, 4, 1, 2]] 
 

Time Complexity: O(n*n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list

Method #2 : Using union() + list comprehension 

A combination of the above methods can be used to perform this task. In this, we compute the union using union() rather than the nested loops. The result is compiled using list comprehension.

Python3




# Python3 code to demonstrate working of
# Flatten Nested Dictionary to Matrix
# using union() + list comprehension
 
# initializing dictionary
test_dict = {'Gfg1': {'CS': 1, 'GATE': 2},
             'Gfg2': {'CS': 2, 'GATE': 3},
             'Gfg3': {'CS': 4, 'GATE': 5}}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Flatten Nested Dictionary to Matrix
# using union() + list comprehension
temp = set().union(*test_dict.values())
res = [list(test_dict.keys())]
res += [[key] + [sub.get(key, 0) for sub in test_dict.values()]
        for key in temp]
 
# printing result
print("The Grouped dictionary list is : " + str(res))


Output : 
The original dictionary is : {‘Gfg3’: {‘GATE’: 5, ‘CS’: 4}, ‘Gfg1’: {‘GATE’: 2, ‘CS’: 1}, ‘Gfg2’: {‘GATE’: 3, ‘CS’: 2}} 
The Grouped dictionary list is : [[‘Gfg3’, ‘Gfg1’, ‘Gfg2’], [‘GATE’, 5, 2, 3], [‘CS’, 4, 1, 2]] 
 

 Method #3: Flatten a nested dictionary to a matrix using pandas library

Use the pandas library to flatten a nested dictionary into a matrix. It creates a DataFrame from the dictionary, fills the missing values with 0, and then converts it to a list of lists where each sublist represents a row in the matrix. The first sublist contains the keys of the inner dictionaries, and the remaining sublists contain the corresponding values.

Python3




import pandas as pd
 
# initializing dictionary
test_dict = {'Gfg1': {'CS': 1, 'GATE': 2},
             'Gfg2': {'CS': 2, 'GATE': 3},
             'Gfg3': {'CS': 4, 'GATE': 5}}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Flatten Nested Dictionary to Matrix using pandas DataFrame
df = pd.DataFrame(test_dict).T.fillna(0)
 
# printing result
res = df.reset_index().rename(columns={'index': 'Grouped'})
res = [list(res.columns)] + res.values.tolist()
print("The Grouped dictionary list is : " + str(res))


OUTPUT:

The original dictionary is : {'Gfg1': {'CS': 1, 'GATE': 2}, 'Gfg2': {'CS': 2, 'GATE': 3}, 'Gfg3': {'CS': 4, 'GATE': 5}}
The Grouped dictionary list is : [['Grouped', 'CS', 'GATE'], ['Gfg1', 1, 2], ['Gfg2', 2, 3], ['Gfg3', 4, 5]]

Time complexity: O(nm), where n is the number of keys in the outer dictionary and m is the number of keys in the inner dictionary with the most number of keys.
Auxiliary space: O(nm), as it creates a pandas.

Method #4: Using dictionary comprehension and itertools.chain() function

The program initializes a nested dictionary, flattens it to a matrix using dictionary comprehension and itertools.chain() function, and prints the original dictionary and the resulting matrix.

  1. Import the itertools module.
  2. Define the nested dictionary test_dict.
  3. Use dictionary comprehension and itertools.chain() function to flatten the nested dictionary to a matrix.
  4. Create a list res that consists of the headers for the table, followed by the data rows.
  5. Print the resulting matrix.

Python3




import itertools
 
# initializing dictionary
test_dict = {'Gfg1' : {'CS':1, 'GATE' : 2},
             'Gfg2' : {'CS':2, 'GATE' : 3},
             'Gfg3' : {'CS':4, 'GATE' : 5}}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using dictionary comprehension and itertools.chain() function
res = [['Grouped', 'CS', 'GATE']] + [[k] + list(v.values()) for k, v in test_dict.items()]
 
# printing result
print("The Grouped dictionary list is : " + str(res))


Output

The original dictionary is : {'Gfg1': {'CS': 1, 'GATE': 2}, 'Gfg2': {'CS': 2, 'GATE': 3}, 'Gfg3': {'CS': 4, 'GATE': 5}}
The Grouped dictionary list is : [['Grouped', 'CS', 'GATE'], ['Gfg1', 1, 2], ['Gfg2', 2, 3], ['Gfg3', 4, 5]]

Time complexity: O(n), where n is the number of elements in the dictionary.
Auxiliary space: O(n), where n is the number of elements in the dictionary, to store the resulting matrix.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments