I have a dataframe with two categorical columns and a third with integers:
import pandas as pd
df1 = pd.DataFrame({
'First': ['A','A','A','B','B','C'],
'Second': ['B','C','D','C','D','D'],
'Value': [1,2,3,4,5,6]}
)
df1
First Second Value
0 A B 1
1 A C 2
2 A D 3
3 B D 4
4 B D 5
5 C D 6
I would like to get the corresponding triangular matrix, as (missing values can be NAs):
A B C D
1 2 3 A
4 5 B
6 C
and finally, plot it in a triangular heatmap, which I believe I could be able to do with the help of this question, which, however, requires a numpy masked array as input. Other solutions to plot this without using numpy are also very welcome.
Any pythonic ideas of how to achieve this?
EDIT:
I realized the example I gave was too neat. My columns are not organized in the way they are above. So I have something like this:
df1 = pd.DataFrame({
'First': ['D','C','B','A','C','A','B','D','B','C'],
'Second': ['E','E','C','D','D','E','E','B','A','A'],
'Value': [1,2,3,4,5,6,7,8,9,10]}
)
First Second Value
0 D E 1
1 C E 2
2 B C 3
3 A D 4
4 C D 5
5 A E 6
6 B E 7
7 D B 8
8 B A 9
9 C A 10
and
df1.pivot('First','Second','Value')
produces
Second A B C D E
First
A NaN NaN NaN 4.0 6.0
B 9.0 NaN 3.0 NaN 7.0
C 10.0 NaN NaN 5.0 2.0
D NaN 8.0 NaN NaN 1.0
A non-triangular, non-simmetrical matrix. I need to have the same number of rows and columns and to push all these NaNs to the edges to create a triangle. Pivot doesn't seem to be a possible solution
EDIT2
The solution and desired output exists and is:
A B C D E
A NaN 9 10 4 6
B NaN NaN 3 8 7
C NaN NaN NaN 5 2
D NaN NaN NaN NaN 1
E NaN NaN NaN NaN NaN