I have a Pandas DataFrame like this:
var value
0 a 11
1 a 12
2 a 13
3 b 21
4 b 22
5 b 23
6 c 31
7 c 32
8 c 33
This is in long format, and I want to go to wide format, transforming the value for the column var
into columns, like this:
a b c
0 11 21 31
1 12 22 33
2 31 32 33
The problem is that I do not have an explicit column for what would be the new index, so I cannot use pivot()
.
I imagine that if I create a new column that counts the nth occurrence of a value for a given var
, I can use that as my new index for pivot()
:
i var value
0 0 a 11
1 1 a 12
2 2 a 13
3 0 b 21
4 1 b 22
5 2 b 23
6 0 c 31
7 1 c 32
8 2 c 33
How do I do that? The issue is I don't know the terminology so it's hard to look up. Also, if there is any better way to do this, please let me know.