Dictionary with a tuple in a data block

I have a python dictonary with keys as dates and values ​​as a 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)}

It is required to write this to a Dataframe with a constant (userid) as 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 to convert the dictionary to a list or a numpy array for writing in a Dataframe, but a tuple in the dictionary, I cannot separate them. Any ideas?

+4
source share
2 answers

You can use DataFrame.from_dictwith DataFrame.insertif you want to select the position of a 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 a new column at the end is required 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 a 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 comments:

dict comprehension, 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
+3

- :

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: dict dict.

0

Source: https://habr.com/ru/post/1668305/


All Articles