Home > Back-end >  How can I create a column of numbers that ascends after a certain amount of rows?
How can I create a column of numbers that ascends after a certain amount of rows?

Time:08-05

I have a column of scores going in descending order. I want to create a column of difficulty level with scale 1-10 going up every 37 rows for diffculty 1-7 and then 36 rows for 8-10. i have created a small example below where the difficulty goes down in 3 row intervals and the final difficulty '4' and '5' is 2 rows

In:
   score
0   11
1   10
2   9
3   8
4   8
5   6
6   5
7   4
8   4
9   3
10  2
11  1
12  1

Out:

   score  difficulty  
0  11      1  
1  10      1  
2  9       1 
3  8       2
4  8       2
5  6       2
6  5       3
7  4       3
8  4       3
9  3       4
10 2       4
11 1       5
12 1       5

CodePudding user response:

If I understand your problem correctly, you could do something like:

import pandas as pd
from random import randint

count = (37*7)   (36*3)
difficulty = [int(i/37)   1 for i in range(37*7)]   [int(i/36)   8 for i in range(36*3)]
df = pd.DataFrame({'score': [randint(0, 10) for i in range(count)]})
df['difficulty'] = difficulty

  • Related