Search code examples
pandasdictionaryexport-to-excel

From dictionary to organized Excel


Good Morning,

I have a dictionary: organized this way below; And What I want to do is use the dictionary values as the column number for the Key. As showed below:

My first idea was to loop through the dictionary and create a text file where dico_values = tabs and then transform this new file into an excel file but this seems one too much step. Cheers to all

Raw Dico

enter image description here


Solution

  • You could perhaps try this:

    new_dico = {value: [] for value in dico.values()}  # {1: [], 2: [], 3: [], ...}
    for key, value in dico.items():
        new_dico[value].append(key)
        for otherkey in new_dico.keys():
            if otherkey == value:
                continue
            else:
                new_dico[otherkey].append("")
    
    # new_dico == {1: ["President of ...", "Members of ..", "", ...], 2: ["", "", "President's Office", ...], ...}
    
    # Then you can make a dataframe of 'new_dico' with pandas, for instance