PyRaider
PyRaider

Reputation: 689

dictionary with tuple into a dataframe

I have a python dictonary with keys as dates and values as tuple as shown below.

dct = {'01/24/2017 01:10:23.1230':('a',12),
        '12/25/2016 10:12:45.128':('b',23),
        '11/16/2016 09:39:55.459':('c',45),
        '01/12/2017 15:55:20.783':('d',34)}

Wanted to write this into a Dataframe with a constant (userid), something like shown below.

   userid   Date                            value1                value2
0  123     '01/24/2017 01:10:23.1230'        a                     12
1  123     '12/25/2016 10:12:45.128'         b                     23
2  123     '11/16/2016 09:39:55.459'         c                     45
3  123     '01/12/2017 15:55:20.783'         d                     34

tried converting the dictionary to a list or a numpy array to write to Dataframe but the tuple in the dictionary, I am not able to separate them out. Any ideas?

Upvotes: 6

Views: 5799

Answers (2)

jezrael
jezrael

Reputation: 863531

You can use DataFrame.from_dict with DataFrame.insert if need choose position of new column:

d = {'01/24/2017 01:10:23.1230':('a',12),'12/25/2016 10:12:45.128':('b',23),'11/16/2016 09:39:55.459':('c',45),'01/12/2017 15:55:20.783':('d',34)}
df = pd.DataFrame.from_dict(d, orient='index').reset_index()
df.columns = ['Date','value1','value2']
df.insert(0, 'userid', 123)
print (df)
   userid                      Date value1  value2
0     123  01/24/2017 01:10:23.1230      a      12
1     123   12/25/2016 10:12:45.128      b      23
2     123   01/12/2017 15:55:20.783      d      34
3     123   11/16/2016 09:39:55.459      c      45

If need new column to the end of DataFrame:

df['userid'] = 123
print (df)
                       Date value1  value2  userid
0  01/24/2017 01:10:23.1230      a      12     123
1   12/25/2016 10:12:45.128      b      23     123
2   01/12/2017 15:55:20.783      d      34     123
3   11/16/2016 09:39:55.459      c      45     123

Or solution with assign:

df = df.assign(userid=123)
print (df)
                       Date value1  value2  userid
0  01/24/2017 01:10:23.1230      a      12     123
1   12/25/2016 10:12:45.128      b      23     123
2   01/12/2017 15:55:20.783      d      34     123
3   11/16/2016 09:39:55.459      c      45     123

EDIT by comment:

Use dict comprehension where add new value 123:

d1 = {k:(123, v[0], v[1]) for k,v in d.items()}
print (d1)
{'01/24/2017 01:10:23.1230': (123, 'a', 12), 
'11/16/2016 09:39:55.459': (123, 'c', 45), 
'01/12/2017 15:55:20.783': (123, 'd', 34), 
'12/25/2016 10:12:45.128': (123, 'b', 23)}

df = pd.DataFrame.from_dict(d1, orient='index').reset_index()
df.columns = ['Date','userid','value1','value2']
print (df)
                       Date  userid value1  value2
0  01/24/2017 01:10:23.1230     123      a      12
1   11/16/2016 09:39:55.459     123      c      45
2   01/12/2017 15:55:20.783     123      d      34
3   12/25/2016 10:12:45.128     123      b      23

Upvotes: 5

Zeugma
Zeugma

Reputation: 32125

Something like this:

pd.DataFrame(data=dct).T.reset_index()
Out[13]: 
                      index  0   1
0   01/12/2017 15:55:20.783  d  34
1  01/24/2017 01:10:23.1230  a  12
2   11/16/2016 09:39:55.459  c  45
3   12/25/2016 10:12:45.128  b  23

PS: don't use dict as a variable name or you're superseding the dict class.

Upvotes: 0

Related Questions