Home > Blockchain >  filter column of a dataframe by the two fields that are repeated the most
filter column of a dataframe by the two fields that are repeated the most

Time:03-24

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

CodePudding user response:

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]

CodePudding user response:

You may use value_counts

pd_data.value_counts()

or

pd_data.groupby(['label']).count()
  • Related