I have a nested dictionary that I want to turn into a dataframe. When I use
pd.DataFrame(my_dict)
It modifies the order of the columns to be in alphabetical order. I want the order to be as input.
There is a problem almost exactly like this here:
Pandas: create dataframe without auto ordering column names alphabetically
The accepted answer has two solutions. The first solution in my case I do not think would work or would at least be tedious and not as readable because my dictionary is nested and much longer than his.
The second solution involves using collections.OrderedDict
to create an ordered dict which is then converted into a dataframe. This supposedly should fix the problem but it does not for me. The dataframe is still ordered alphabetically. I think it may have to do with the fact that my dictionary is nested. I tried using collections.OrderedDict
on all nested dicts and it still did not work. Well it worked, but did not change my sorted column issue. Here is my code:
my_dict = collections.OrderedDict()
code code code
for fname in os.listdir(myfile)
labels = collections.OrderedDict({A : 1, C : 2, B : 3, etc})
my_dict.update({fname : labels})
Obviously this is very simplified, but it shows the general idea. I make an empty ordered dictionary, then sort through a file and collect labels with values and store them in an ordered dictionary, then update the my_dict with the fname and labels ordered dict.
The dataframe that is output when I use pd.DataFrame(my_dict).T
orders the columns (e.g. A,C,B) in alphabetical order. I would like it to be in input order.
If you know why my dataframe is still auto-sorting alphabetically or of another way to sort this please let me know!