I am using pandas to write data to an excel file. I want to dump data as it is in one sheet and in second sheet I want it in sorted way. Layers is the key and weights are the values in the dictionary.
for e.g. sheet1 should have the table as below (unsorted) :
and sheet2 should have sorted entries:
I already tried OrderedDict
df1 = pd.DataFrame.from_dict(dict_weights, orient="index")
df1.columns = ['weights']
df2 = pd.DataFrame.from_dict(collections.OrderedDict(dict_weights), orient="index")
df2.columns = ['weights']
df1.to_excel(writer, sheet_name='sheet1')
df2.to_excel(writer, sheet_name='sheet2', startcol=3)
writer = pd.ExcelWriter(filename, engine='xlsxwriter')
writer.save()
The problem is it do the sorting but on both sheets. I just want data to be sorted in sheet2 and in sheet1 it should remain unsorted.
Expected Output in :
Sheet1
Layer; weights
T1_max_pool; 4
activation_9; 1
sum_9; 3
Merge_2; 4
activation_2; 1
T2_max_pool; 4
Sheet2
Layer; weights
activation_2; 1
activation_9; 1
Merge_2; 4
sum_9; 3
T1_max_pool; 4
T2_max_pool; 4
Any suggestions?? :) Thanks!


OrderedDictjust remembers the order of insertion however it is not sorted (at least not in an intuitive way). You can create theOrderedDictfirst and then insert items one after another in a sorted manner. I.e. instead ofOrderedDict(dict_weights)you can dosorted_dict = OrderedDict(); for key in sorted(dict_weights): sorted_dict[key] = dict_weights[key].