Sorry in advance if this question has been answered before but I can't seem to find it.
I have panda dataframe like so:
id | value1 | value2 | ... | valueN
1 | 321 | 44 | ... | 7766
2 | 5678 | 7638 | ... | 987423
2 | 0971 | 7638 | ... | 1
and so on...
I load it correctly and what I want to achieve is an OrderedDict
which will collapse the double values if needed. For the above example,
the output dictionary should be:
{1: ['321', '44', ..., '7766'], 2:['5678,0971', '7638', ..., '987423,1']}
Notice that the values of the dictionary are list
and the values of the list are strings
.
My code so far is:
od = collections.OrderedDict()
for k in df.id:
if k in od:
# This key, pre-exists in this dictionary, so we have to append values
# what should I do here?
else:
# new value inserted. proceed.
od[k] = unordered_dict.get(k)
any ideas?