0

Here is my dataframe:

score 
1
62
7
15
167
73
25
24
2
76

I want to compare a score with the previous 4 scores and count the number of scores higher than the current one.

This is my expected output:

score   count 
1
62
7
15
167       0
73        1    (we take 4 previous scores : 167,15,7,62 there is just 167 > 73 so count 1)
25        2    
24        3
2         4
76        0

If somebody has an idea on how to do that, you are welcome

Thanks!

Kben59
  • 378
  • 2
  • 10
  • Are you sure your output is correct? Because if you would only consider the previous 4 scores, then for `25` you would have `2` as output and not `3`, and so on... It looks like you consider all the previous numbers. – mabergerx Jul 02 '20 at 15:49
  • Does this answer your question? [Pandas count values greater than current row in the last n rows](https://stackoverflow.com/questions/51039857/pandas-count-values-greater-than-current-row-in-the-last-n-rows) – Henry Yik Jul 02 '20 at 15:51

1 Answers1

1

I do not think your output is according to your question. However, if you do look only at the previous 4 elements, then you could implement the following:

scores = [1, 62, 7, 15, 167, 73, 25, 24, 2, 76]
highers = []

for index, score in enumerate(scores[4:]):
    higher = len([s for s in scores[index:index+4] if score < s])
    print(higher)
    highers.append(higher)

print(highers)
# [0, 1, 2, 3, 4, 0]

Then, you could just add this highers list as a pandas column:

df['output'] = [0]*4 + highers

Note that I pad the output in such way here that I assign zeros to the first four values.

mabergerx
  • 1,216
  • 7
  • 19