Q:

Pandas Dataframe: limit the number of rows with a common subset value

I have this data set (outputted in a .csv file):
email, link
0,,
1, hello@dog.com, dog.com
2, bark@dog.com, dog.com
3, growl@dog.com, dog.com
4, meow@cat.net, cat.net
5, purr@cat.net, cat.net,
6, sleep@cat.net, cat.net
7, scream@monkey.eu, monkey.eu
8, run@horse.com, horse.com

As you can see, some links are the same while the email is always unique. I would like to keep maximum 2 rows with the same link, removing the third and the subsequents, like this:
email, link
0,,
1, hello@dog.com, dog.com
2, bark@dog.com, dog.com
3, meow@cat.net, cat.net
4, purr@cat.net, cat.net,
5, scream@monkey.eu, monkey.eu
6, run@horse.com, horse.com

How to do it? I tried with this solution, but it only outputs the links. Merging it with the email address mess everythingup because of the different lenght of the subsets (lists):
from collections import Counter

def keep_n_dupes(remove_from, how_many):
    counts = Counter()
    for item in remove_from:
        counts[item] += 1
        if counts[item] <= how_many:
            yield item
new_links = list(keep_n_dupes(df['link'], 2))

A:

Use groupby.head:
df.groupby('link').head(2)

          email         link
0     hello@dog.com    dog.com
1      bark@dog.com    dog.com
3      meow@cat.net    cat.net
4      purr@cat.net    cat.net
6  scream@monkey.eu  monkey.eu
7     run@horse.com  horse.com

