'How can I delete some rows according to different numbers

pretty new to python. I'm currently trying to modify a dataframe that looks like this

 stations  level_1   distance
    6          1     0.870363
    6          2     1.851696
    6          3     2.019918
    8          1     1.261097
    8          2     1.16174
    8          3     1.261097
    25         1     1.14675
    25         2     1.83467
    25         3     1.83602
    

as you can see, each station has 3 types of level and corresponding distance. What I want to achieve is, delete some number of rows according to both 'stations' and 'level_1'.

For example for station 6, I want only 2 of level_1 to stay, for station 8, I want 2 and 3 of level_1 to stay and for station 25, I only want 3 to stay.

Consequently I want my dataframe to look like this

stations  level_1   distance
    6          2     1.851696
    8          2     1.16174
    8          3     1.261097
    25         3     1.83602

This is not my real data though, I have 137 stations and each station has 954 levels so I need a pythonic way to achieve this. So far, I couldn't come up with something. Thanks in advance!

Edit: The logic to delete the rows is different for each station. I have created another dataframe for that. Looks like this: 1



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source