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")
It appears as though a simpler option is to use pivot_table
function in pandas:
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']}
tab4 = pd.DataFrame(data=d)
tab4.pivot_table(
values='value',
index=['id1', 'id2'],
columns='type')
yielding the following, which I believe is what you are after:
type A B C
id1 id2
1 1 1 2 3
2 4 5 6
2 1 7 8 9
This was found through this question earlier post on the subject.
NOTE: this is my first answer on SO, so please let me know if I should be doing anything differently!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.