1

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?

  • I don't get what the problem is. `{'A':'B', 'C':'F', 'D':'E'} == {'A':'B', 'D':'E', 'C':'F'}` Why ordering is needed? –  Jul 26 '17 at 03:24
  • Because the 'A', 'B', 'C', 'D', 'E', 'F' are all json extracted from a PDF...those were examples...A could be Name, B could be last-name...then comes D which is "age"...E is #age... – neutralCreep Jul 26 '17 at 03:55

1 Answers1

0

dictionaries are inherently unordered. Therefore, the two dictionaries are equivalent. You may want to consider OrderedDict from the collections module

from collections import OrderedDict

OrderedDict(zip(df.Content.iloc[::2], df.Content.iloc[1::2]))

OrderedDict([(u'A', u'B'), (u'C', u'F'), (u'D', u'E')])

It behaves like a dictionary but maintains order.

As opposed to:

dict(zip(df.Content.iloc[::2], df.Content.iloc[1::2]))

{u'A': u'B', u'C': u'F', u'D': u'E'}

Which doesn't care about order.

piRSquared
  • 285,575
  • 57
  • 475
  • 624