Jake Han
Jake Han

Reputation: 93

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.

Upvotes: 4

Views: 864

Answers (3)

Scott Boston
Scott Boston

Reputation: 153460

Let's try this:

Correct some data:

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

Flatten tuples

l = [(i[0],i[1],v) for i,v in td]
lol = [list(e) for e in l]

Create and reshape dataframe

pd.DataFrame(lol).set_index([1,0]).rename_axis([None,None]).unstack()[2]\
  .fillna(0).astype(int)

Output:

   1  2  3
1  1  1  1
2  2  5  2
3  1  0  0

To expand dataframe:

pd.DataFrame(lol).set_index([1,0]).rename_axis([None,None]).unstack()[2]\
  .reindex(index=np.arange(1,10), columns=np.arange(1,10)).fillna(0).astype(int)

Output:

   1   2  3  4  5  6  7  8  9
1  9   0  0  0  0  0  0  0  0
2  0  10  0  0  0  0  0  0  0
3  0   1  0  1  0  0  0  0  0
4  0   0  0  1  0  0  0  0  0
5  0   0  0  0  1  1  0  0  0
6  0   0  0  0  0  1  0  0  0
7  0   0  0  0  0  0  0  0  0
8  0   0  0  0  0  0  0  0  0
9  0   0  0  0  0  0  0  0  0

Upvotes: 3

BENY
BENY

Reputation: 323226

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

Upvotes: 4

cs95
cs95

Reputation: 402263

I don't see the need for MultiIndex here.

Option 1
Flatten before calling pd.DataFrame. You can generalise this with * argument unpacking -

pd.DataFrame([list(x) + y for x, *y in td])

   0  1  2
0  1  1  1
1  1  2  2
2  1  3  1
3  2  1  1
4  2  2  5
5  3  2  2

Option 2
Slightly more roundabout, using pd.concat -

df = pd.DataFrame(td)

        0  1
0  (1, 1)  1
1  (1, 2)  2
2  (1, 3)  1
3  (2, 1)  1
4  (2, 2)  5
5  (3, 2)  2

pd.concat([pd.DataFrame(df.iloc[:, 0].tolist()), df.iloc[:, 1:]], axis=1)

   0  1  1
0  1  1  1
1  1  2  2
2  1  3  1
3  2  1  1
4  2  2  5
5  3  2  2

Upvotes: 4

Related Questions