Home > Net >  How to remove rows of columns whose value count is less than particular number?
How to remove rows of columns whose value count is less than particular number?

Time:02-20

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.

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.

CodePudding user response:

You can do this below;

column = df['Brand'] > 6
valueCount = column.value_counts()
  • Related