How to limit by column repetitions in Python Pandas
Question: I want to select only rows that contain fewer than 3 total repetitions of an
element in a column. To be specific, I have a large directory of phone
numbers, names, and cities. I want to export a list of only "small cities,"
such that any line with a city that has fewer than three entries across the
document is kept. So, for example

    
    
    Name, City, State
    Foo, L.A., CA
    Bar, L.A., CA
    Sam, L.A., CA
    Tricia, Kent, WA
    Bob, Kent, WA
    Ida, Boo, PA
    Monster Mash, Whack, PA
    Zoomacroom, L.A., CA
    Otter Pop, Boo, PA
    Snake, HP, WA
    Ronnie the Bear, Boo, PA
    

Should become:

    
    
    Name, City, State
    Tricia, Kent, WA
    Bob, Kent, WA
    Snake, HP, WA
    Monster Mash, Whack, PA
    

I also don't have to use pandas - I can use csv just as easily; I just happen
to already have it imported in my cleaning script.


Answer: How about something like:

    
    
    >>> small_cities = df.groupby(["City", "State"]).filter(lambda x: x.count() < 3)
    >>> small_cities
               Name   City State
    3        Tricia   Kent    WA
    4           Bob   Kent    WA
    6  Monster Mash  Whack    PA
    9         Snake     HP    WA
    
    [4 rows x 3 columns]
    

