user2333196 user2333196 - 1 year ago 51
Python Question

pandas filter if name appears in column more than n times

this is my dataframe

df = pd.DataFrame({'Col1':['Joe','Bob','Joe','Joe'],

I only want the names of if it appears more than once in 'Col1'

I can do it like this

grouped = df.groupby("Col1")
grouped.filter(lambda x: x["Col1"].count()>2)['Col1'].unique()

However that is ugly looking code

Is there simpler cleaner way?

Answer Source

Use value_counts and isin

vc = df.Col1.value_counts() > 2
vc = vc[vc]


enter image description here