如何获取一列中最频繁出现的值的数量? [英] How to get the number of the most frequent value in a column?
问题描述
我有一个数据框,我想知道给定列具有最频繁值的次数.
I have a data frame and I would like to know how many times a given column has the most frequent value.
我尝试通过以下方式进行操作:
I try to do it in the following way:
items_counts = df['item'].value_counts()
max_item = items_counts.max()
结果是:
ValueError: cannot convert float NaN to integer
据我了解,在第一行中,我得到了一系列,其中列中的值用作键,而这些值的频率用作值.因此,我只需要在系列中找到最大的值,由于某种原因,它是行不通的.有人知道如何解决这个问题吗?
As far as I understand, with the first line I get series in which the values from a column are used as key and frequency of these values are used as values. So, I just need to find the largest value in the series and, because of some reason, it does not work. Does anybody know how this problem can be solved?
推荐答案
该列中可能包含一些空值.您可以使用df = df.dropna(subset=['item'])
删除它们.然后df['item'].value_counts().max()
应该为您提供最大计数,而df['item'].value_counts().idxmax()
应该为您提供最频繁的值.
It looks like you may have some nulls in the column. You can drop them with df = df.dropna(subset=['item'])
. Then df['item'].value_counts().max()
should give you the max counts, and df['item'].value_counts().idxmax()
should give you the most frequent value.
这篇关于如何获取一列中最频繁出现的值的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!