Question: I've got a matrix like this:

```
df = pd.DataFrame({'a':[7, 0, 3], 'b':[0, 4, 2], 'c':[3, 2, 9]})
df.index = list(df)
df

    a   b   c
a   7   0   3
b   0   4   2
c   3   2   9

```

And I'd like to get something like this:

```
C1  C2  V
0   a   a   7
1   a   b   0
2   a   c   3
3   b   a   0
4   b   b   4
5   b   c   2
6   c   a   3
7   c   b   2
8   c   c   9

```

For which I've written the following code:

```
vv = pd.DataFrame(columns=['C1', 'C2', 'V'])
i = 0
for cat1 in df.index:
    for cat2 in df.index:
        vv.loc[i] = [cat1, cat2, d[cat1][cat2]]
        i += 1
vv['V'] = vv['V'].astype(int)

```

Is there a better/faster/more elegant way of doing this?

Answer: 
```
In [90]: df = df.stack().reset_index()

In [91]: df.columns = ['C1', 'C2', 'v']

In [92]: df
Out[92]: 
  C1 C2  v
0  a  a  7
1  a  b  0
2  a  c  3
3  b  a  0
4  b  b  4
5  b  c  2
6  c  a  3
7  c  b  2
8  c  c  9

```
Use:

```
df = (df.rename_axis('C2')
        .reset_index()
        .melt('C2', var_name='C1', value_name='V')
        .reindex(columns=['C1','C2','V']))
print (df)
  C1 C2  V
0  a  a  7
1  a  b  0
2  a  c  3
3  b  a  0
4  b  b  4
5  b  c  2
6  c  a  3
7  c  b  2
8  c  c  9

```