I know there are different variations of my question. But I hope mine is different in some way and doesn't get flagged. Using Python 2.7, Pandas, Dictionaries. I have a dataframe, closely resembling the following:
boxNumber Content
[1.0, 2.0] A
[2.0, 4.5] B
[2.5, 3.0] C
[1.5, 2.5] F
[1.4, 4.5] D
[1.3, 3.2] E
Now I would have to obtain a dictionary like {A:B, C:F, D:E}. I go about this, in the following way.I have shifted this into a pandas dataframe, dropped all null valued rows.
keys = ['A', 'B', 'C', 'F','D', 'E']
test1 = df[df.Content.str.match('A').shift(1).fillna(False)]
test2 = df[df.Content.str.match('C').shift(1).fillna(False)]
test3 = df[df.Content.str.match('D').shift(1).fillna(False)]
values = [test1.Content.iloc[0], test2.Content.iloc[0],test3.Content.iloc[0]
item1 = dict(zip(keys, values))
print(item1)
My output is
{'A':'B', 'D':'E', 'C':'F'}
But I need
{'A':'B', 'C':'F', 'D':'E'}
As a dict is orderless in python 2.7, my final output also becomes orderless! An OrderedDict() is no good. It needs to be a normal dict. Is there any solution to this? Or should I just drop using Pandas?