Код: Выделить всё
dic = {'id':[1,1,1,1,1,2,2,2,2], 'sales': [100.00, 200.00, 300.00, 400.00, 500.00, 100.00, 200.00, 300.00, 400.00], 'year_month': [202201, 202202, 0, 202204, 202205, 202201, 202202, 202203, 0]}
df = pd.DataFrame(dic)
Код: Выделить всё
id sales year_month
0 1 100.0 202201
1 1 200.0 202202
2 1 300.0 0
3 1 400.0 202204
4 1 500.0 202205
5 2 100.0 202201
6 2 200.0 202202
7 2 300.0 202203
8 2 400.0 0
Код: Выделить всё
id sales year_month rank
0 1 100.0 202201 -2
1 1 200.0 202202 -1
2 1 300.0 0 0
3 1 400.0 202204 1
4 1 500.0 202205 2
5 2 100.0 202201 -3
6 2 200.0 202202 -2
7 2 300.0 202203 -1
8 2 400.0 0 0
Подробнее здесь: https://stackoverflow.com/questions/732 ... alue-by-id