Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Table by using Tuple Dictionary (Python Pandas)

I have dict with tuple key:

td=[((1, 1), 1), ((1, 2), 2), ((1, 3), 1) ((2, 1), 1), ((2, 2), 5), ((3, 2), 2]

I want to create the table like below: (using tuple as index)

     1     2     3

1    1     2     1

2    2     5     2

3    1     0     0 

How can I create this table by using python?

I tried pd.MultiIndex, but it was not working.

Thanks.

like image 648
Jake Han Avatar asked Dec 18 '22 02:12

Jake Han


1 Answers

I think this way .

pd.Series(dict(td)).reset_index()
Out[115]: 
   level_0  level_1  0
0        1        1  1
1        1        2  2
2        1        3  1
3        2        1  1
4        2        2  5
5        3        1  1
6        3        2  2
like image 145
BENY Avatar answered Dec 27 '22 03:12

BENY