Ad

How To Remove Rows Of Columns Whose Value Count Is Less Than Particular Number?

- 1 answer

df['Brand'].value_counts() gives list of occurrence of each value in column Brand. I want to remove all rows where the occurrence is less than 6. Column Brand is string.

Table

Ad

Answer

Use:

df = pd.DataFrame({'Brand':[1,2,3,3,3,3,3,3,3,3]})
df[df.apply(lambda x: df.value_counts()[x]>6)['Brand'].values]

Output:

enter image description here

A more efficient way, if your data size is huge:

temp = df.value_counts()>6
df[df['Brand'].isin(temp[temp].index.get_level_values(0).values)]

output:

enter image description here

Another way:

df = pd.DataFrame({'Brand':[1,2,3,3,3,3,3,3,3,3]})
temp = df['Brand'].tolist()
df[df['Brand'].apply(lambda x: temp.count(x)>6)]

with the same output.

Ad
source: stackoverflow.com
Ad