I have a nested dictionary that contains (nested lists). I have gone through several posts in the Stackoverflow (here, here, and here). But, I am not getting an idea, how to solve the issues. The dictionary looks like
{ 'LinReg': { 'First': [ array([ 0.83333333, -0.77777778, -0.6 , 0.72222222]),
array([0.4 , 0.05555556, 0.4 , 0.44444444])],
'Second': [ array([[5.16666667, 3.28571429, 2.4 , 6.38461538]]),
array([[ 4.83333333, 23. , 1.26666667, 3.22222222]])],
'Third': [ array([[ 5.16666667, -3.28571429, -2.4 , 6.38461538]]),
array([[ 4.83333333, 23. , 1.26666667, 3.22222222]])],
'Fourth': [ array([0.83333333, 0.77777778, 0.6 , 0.72222222]),
array([0.4 , 0.05555556, 0.4 , 0.44444444])]},
'kNN': { 'First': [ array([ 0. , -0.75 , 0.5 , 0.41666667]),
array([ 0. , -0.8 , -0.1 , 0.08333333])],
'Second': [ array([[1. , 2.33333333, 4. , 7.4 ]]),
array([[ inf, 2.75 , 1.4 , 1.41666667]])],
'Third': [ array([[ 0. , -2.33333333, 4. , 7.4 ]]),
array([[ 1. , -2.75 , -1.4 , 1.41666667]])],
'Fourth': [ array([0. , 0.75 , 0.5 , 0.41666667]),
array([0. , 0.8 , 0.1 , 0.08333333])]}}
The code I am trying
a = ["Album/Track"] + dictionary.keys()
x = list(set([y for z in dictionary.values() for y in z.keys()]))
rows = [a] + [[q] + [dictionary[p].get(q, "-") for p in a[1:]] for q in x]
with open("my.csv", "wb") as csvfile:
writer = csv.writer(csvfile)
for row in rows:
writer.write(row)
How can I convert this dictionary into a CSV file like the below one?
LinReg First Array1_Output1, Array1_Output2, and so on
Array2_Output1, Array2_Output2, and so on
Second
Third
Fourth
kNN First Array1_Output1, Array1_Output2, and so on
Array2_Output1, Array2_Output2, and so on
Second
Third
Fourth