I have a df with two columns x and y . Column y is cum count of x values. x values have different counts. How do I get a result df of top two y counts for each x without iterating through rows.
Example df:
df = pd.DataFrame({"x": [101, 101, 101, 101, 201, 201, 201, 405, 405], "y": [1, 2, 3, 4, 1, 2, 3, 1, 2]})
x y
0 101 1
1 101 2
2 101 3
3 101 4
4 201 1
5 201 2
6 201 3
7 405 1
8 405 2
Desired result:
x y
101 3
101 4
201 2
201 3
405 1
405 2