Reputation: 4608
I have a pandas dataframe as follows.
item x1 x2 x3 x4 x5 ratings
item1 x x x x x 46
item1 x x x x x 32
item1 x x x x x 16
item1 x x x x x 6
item1 x x x x x 36
item1 x x x x x 36
item1 x x x x x 12
item1 x x x x x 13
item1 x x x x x 41
item1 x x x x x 42
item1 x x x x x 43
item1 x x x x x 3
item1 x x x x x 76
item1 x x x x x 36
item1 x x x x x 26
item1 x x x x x 12
item1 x x x x x 11
item1 x x x x x 88
item1 x x x x x 87
item1 x x x x x 78
item1 x x x x x 43
item1 x x x x x 42
Now, I want to add another column with rankings
of ratings
.
I did it fine using;
df = df.assign(rankings=df.rank(ascending=False))
I want to re-aggrange ranking column again and add a diffrent column to the dataframe as follows.
1
2
3
Is there a way to do this in pandas in python?
I am happy to provide more details if needed.
Upvotes: 2
Views: 231
Reputation: 862661
Use integer division by 10
and add 1
, last converting to integers:
df = df.assign(rankings=df['ratings'].rank(ascending=False))
df['new'] = (df['rankings'] // 10 + 1).astype(int)
print (df)
item x1 x2 x3 x4 x5 ratings rankings new
0 item1 x x x x x 46 5.0 1
1 item1 x x x x x 32 14.0 2
2 item1 x x x x x 16 16.0 2
3 item1 x x x x x 6 21.0 3
4 item1 x x x x x 36 12.0 2
5 item1 x x x x x 36 12.0 2
6 item1 x x x x x 12 18.5 2
7 item1 x x x x x 13 17.0 2
8 item1 x x x x x 41 10.0 2
9 item1 x x x x x 42 8.5 1
10 item1 x x x x x 43 6.5 1
11 item1 x x x x x 3 22.0 3
12 item1 x x x x x 76 4.0 1
13 item1 x x x x x 36 12.0 2
14 item1 x x x x x 26 15.0 2
15 item1 x x x x x 12 18.5 2
16 item1 x x x x x 11 20.0 3
17 item1 x x x x x 88 1.0 1
18 item1 x x x x x 87 2.0 1
19 item1 x x x x x 78 3.0 1
20 item1 x x x x x 43 6.5 1
21 item1 x x x x x 42 8.5 1
Upvotes: 3