python - Pandas Dataframe Column Manipulation and conversion to Dictionary -


i know there different variations of question. hope mine different in way , doesn't flagged. using python 2.7, pandas, dictionaries. have dataframe, closely resembling following:

boxnumber     content [1.0, 2.0]     [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 have obtain dictionary {a:b, c:f, d:e}. go this, in following way.i have shifted pandas dataframe, dropped 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 need

{'a':'b', 'c':'f', 'd':'e'} 

as dict orderless in python 2.7, final output becomes orderless! ordereddict() no good. needs normal dict. there solution this? or should drop using pandas?

dictionaries inherently unordered. therefore, 2 dictionaries equivalent. may want consider ordereddict 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 dictionary 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 order.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -