Working to understand reshape functions in Pandas. So far I can work with a reshape on a dataframe with a simple structure such as
d = {'id1': [1,1,1,2,2,2], 'id2': [1,1,1,1,1,1], 'value': [1,2,3,4,5,6], 'type':['A','B','C','A','B','C']}
tab = pd.DataFrame(data=d)
tab.pivot(index = 'id1', columns = "type", values = "value")
However, in the situation that looks more like this I'm having some challenges understanding the multi index schema. Conceptually what I would want returned in this case is a new df with 3 rows. The indices are both id1
and id2
such that row 1 would be the values for the unique combination of id1=1
and id2=1
and then row 2 would be id1=1
and id2=2
and last row 3 would be id1=2
and id2=1
.
d = {'id1': [1,1,1,1,1,1,2,2,2], 'id2': [1,1,1,2,2,2,1,1,1], 'value': [1,2,3,4,5,6,7,8,9], 'type':['A','B','C','A','B','C','A','B','C']}
tab = pd.DataFrame(data=d)
tab.pivot(index = 'id1', columns = "type", values = "value")