1

I have a dataframe and values are repeated in a column called label, I want to show only the two that are repeated the most........

I attach an image as an example

I tried

# the name of colums is label

# the name of dataframe is pd_data

filter = \['a','b'\]
pd_data = pd_data\[\~pd_data.labels.isin(filter)\]

print(len(pd_data))
pd_data.groupby('label').size().sort_values(ascending=False)

enter image description here

GABRIEL
  • 13
  • 2
  • Does this answer your question? [Pandas get the most frequent values of a column](https://stackoverflow.com/questions/48590268/pandas-get-the-most-frequent-values-of-a-column) – nathan liang Mar 24 '22 at 00:01

2 Answers2

0

You can use pandas.Series.value_counts() to get the counts of unique values in Series, then slicing the Series with top 2

pd_data['label'].value_counts()[:1]
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
0

You may use value_counts

pd_data.value_counts()

or

pd_data.groupby(['label']).count()
Esraa Abdelmaksoud
  • 1,307
  • 12
  • 25