I have a dataframe in the following format:
customer_id prev_tier tier
0 4 -1 1
1 7 -1 0
2 16 -1 2
3 22 0 -1
4 23 0 -1
5 28 0 1
6 36 0 1
7 38 0 2
8 40 0 2
9 81 1 -1
10 84 1 -1
11 95 1 -1
12 96 1 0
13 97 1 0
14 98 1 2
15 99 1 0
16 108 1 2
17 115 1 2
18 139 2 -1
19 165 2 -1
20 169 2 -1
21 182 2 -1
22 183 2 0
23 184 2 0
24 185 2 0
25 186 2 0
26 199 2 1
27 206 2 1
28 211 2 1
29 225 2 1
I want to create a transition matrix which shows the number of transitions to each tier
from each prev_tier
, such as this:
tier -1 0 1 2
prev_tier
-1 0 1 1 1
0 2 0 2 2
1 3 3 0 3
2 4 4 4 0
What is an elegant way to achieve this in Pandas?