1

I have a pandas dataframe named past_trend which looks like this

   created       moans  thanks
0  2016-12-16     0      0
1  2016-12-17     0      0
2  2016-12-18     0      0
3  2016-12-19     0      2
4  2016-12-20     6      0
5  2016-12-21     0      0
6  2016-12-22     0      2

and I'm trying to convert this to a dictionary which looks something like this

{"moans": [
        ["16 Dec", 0],
        ["17 Dec", 0],
        ["18 Dec", 0],
        ["19 Dec", 2],
        ["20 Dec", 0],
        ["21 Dec", 0],
        ["22 Dec", 2]
    ],
    "thanks": [
        ["16 Dec", 0],
        ["17 Dec", 0],
        ["18 Dec", 0],
        ["19 Dec", 0],
        ["20 Dec", 6],
        ["21 Dec", 0],
        ["22 Dec", 0]
    ]}

the date format does not have to be rigidly like the one shown above, it could be as is also. Thing is when I use the to_dict function I'm getting an output which looks like this

{'created': {0: Timestamp('2016-12-16 00:00:00'),
1: Timestamp('2016-12-17 00:00:00'),
2: Timestamp('2016-12-18 00:00:00'),
3: Timestamp('2016-12-19 00:00:00'),
4: Timestamp('2016-12-20 00:00:00'),
5: Timestamp('2016-12-21 00:00:00'),
6: Timestamp('2016-12-22 00:00:00')},
'moans': {0: 0, 1: 0, 2: 0, 3: 0, 4: 6, 5: 0, 6: 0},
'thanks': {0: 0, 1: 0, 2: 0, 3: 2, 4: 0, 5: 0, 6: 2}}

so I converted the group types(moan,thanks) into a list and am trying to iterate over that. I have gotten this far, as shown below.

#now create the result we want
result = {}
group_types = ['moans', 'thanks']
for group in group_types:
    result[group]={[past_trend['created'],past_trend[group]]}
result

but im getting an error

TypeError: unhashable type: 'list'

3 Answers 3

1

This should do it

{k: [[i.strftime('%d %b'), v] for i, v in s.iteritems()]
 for k, s in df.set_index('created').iteritems()}

{'moans': [['16 Dec', 0],
  ['17 Dec', 0],
  ['18 Dec', 0],
  ['19 Dec', 0],
  ['20 Dec', 6],
  ['21 Dec', 0],
  ['22 Dec', 0]],
 'thanks': [['16 Dec', 0],
  ['17 Dec', 0],
  ['18 Dec', 0],
  ['19 Dec', 2],
  ['20 Dec', 0],
  ['21 Dec', 0],
  ['22 Dec', 2]]}
Sign up to request clarification or add additional context in comments.

Comments

1

Here's on way.

In [99]: {k: [[x, y] for x, y in v.items()] 
            for k, v in df.set_index('created').to_dict().iteritems()}
Out[99]:
{'moans': [['2016-12-22', 0],
  ['2016-12-20', 6],
  ['2016-12-21', 0],
  ['2016-12-19', 0],
  ['2016-12-18', 0],
  ['2016-12-17', 0],
  ['2016-12-16', 0]],
 'thanks': [['2016-12-22', 2],
  ['2016-12-20', 0],
  ['2016-12-21', 0],
  ['2016-12-19', 2],
  ['2016-12-18', 0],
  ['2016-12-17', 0],
  ['2016-12-16', 0]]}

Comments

0

Suppose you start with your dataframe:

In [5]: df
Out[5]: 
     created  moans  thanks
0 2016-12-16      0       0
1 2016-12-17      0       0
2 2016-12-18      0       0
3 2016-12-19      0       2
4 2016-12-20      6       0
5 2016-12-21      0       0
6 2016-12-22      0       2

The easiest thing to do would be to set the index to 'created' then use to_dict:

In [8]: d = df.set_index('created').to_dict()

In [9]: d
   Out[9]: 
   {'moans': {Timestamp('2016-12-16 00:00:00'): 0,
     Timestamp('2016-12-17 00:00:00'): 0,
     Timestamp('2016-12-18 00:00:00'): 0,
     Timestamp('2016-12-19 00:00:00'): 0,
     Timestamp('2016-12-20 00:00:00'): 6,
     Timestamp('2016-12-21 00:00:00'): 0,
     Timestamp('2016-12-22 00:00:00'): 0},
    'thanks': {Timestamp('2016-12-16 00:00:00'): 0,
     Timestamp('2016-12-17 00:00:00'): 0,
     Timestamp('2016-12-18 00:00:00'): 0,
     Timestamp('2016-12-19 00:00:00'): 2,
     Timestamp('2016-12-20 00:00:00'): 0,
     Timestamp('2016-12-21 00:00:00'): 0,
     Timestamp('2016-12-22 00:00:00'): 2}}

If you don't want a dict of dicts, you can always do something like the following:

In [11]: d = {k:sorted(v.items()) for k,v in d.items()}

In [12]: d
Out[12]: 
{'moans': [(Timestamp('2016-12-16 00:00:00'), 0),
  (Timestamp('2016-12-17 00:00:00'), 0),
  (Timestamp('2016-12-18 00:00:00'), 0),
  (Timestamp('2016-12-19 00:00:00'), 0),
  (Timestamp('2016-12-20 00:00:00'), 6),
  (Timestamp('2016-12-21 00:00:00'), 0),
  (Timestamp('2016-12-22 00:00:00'), 0)],
 'thanks': [(Timestamp('2016-12-16 00:00:00'), 0),
  (Timestamp('2016-12-17 00:00:00'), 0),
  (Timestamp('2016-12-18 00:00:00'), 0),
  (Timestamp('2016-12-19 00:00:00'), 2),
  (Timestamp('2016-12-20 00:00:00'), 0),
  (Timestamp('2016-12-21 00:00:00'), 0),
  (Timestamp('2016-12-22 00:00:00'), 2)]}

And if you insist on using strings instead of Timestamp objects (a bad call IMO):

In [13]: {k:[(str(t),e) for t,e in v] for k,v in d.items()}
Out[13]: 
{'moans': [('2016-12-16 00:00:00', 0),
  ('2016-12-17 00:00:00', 0),
  ('2016-12-18 00:00:00', 0),
  ('2016-12-19 00:00:00', 0),
  ('2016-12-20 00:00:00', 6),
  ('2016-12-21 00:00:00', 0),
  ('2016-12-22 00:00:00', 0)],
 'thanks': [('2016-12-16 00:00:00', 0),
  ('2016-12-17 00:00:00', 0),
  ('2016-12-18 00:00:00', 0),
  ('2016-12-19 00:00:00', 2),
  ('2016-12-20 00:00:00', 0),
  ('2016-12-21 00:00:00', 0),
  ('2016-12-22 00:00:00', 2)]}

1 Comment

Thanks a lot juanpa. but really needed it as a dictionary of lists, because the front end of dashboard needed to accept it in this format..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.