其他可能的方法可以使用(i)来自collections模块的Counter, (ii)来自numpy库的unique,以及(iii) pandas中的groupby + size。
使用集合。计数器:
from collections import Counter
out = pd.Series(Counter(df['word']))
使用numpy.unique:
import numpy as np
i, c = np.unique(df['word'], return_counts = True)
out = pd.Series(c, index = i)
使用groupby + size:
out = pd.Series(df.index, index=df['word']).groupby(level=0).size()
value_counts的一个很好的特性是对计数进行排序,但上面的方法中没有这个特性。如果绝对有必要对计数进行排序,那么考虑到它的简单性和性能,value_counts是最好的方法(尽管它的性能仍然略优于其他方法,特别是对于非常大的Series)。
基准
(如果计数排序并不重要):
如果我们看一下运行时,它取决于存储在DataFrame列/Series中的数据。
如果Series是dtype对象,那么对于非常大的Series,最快的方法是集合。Counter,但通常value_counts是非常有竞争力的。
然而,如果它是dtype int,那么最快的方法是numpy.unique:
用于生成图的代码:
import perfplot
import numpy as np
import pandas as pd
from collections import Counter
def creator(n, dt='obj'):
s = pd.Series(np.random.randint(2*n, size=n))
return s.astype(str) if dt=='obj' else s
def plot_perfplot(datatype):
perfplot.show(
setup = lambda n: creator(n, datatype),
kernels = [lambda s: s.value_counts(),
lambda s: pd.Series(Counter(s)),
lambda s: pd.Series((ic := np.unique(s, return_counts=True))[1], index = ic[0]),
lambda s: pd.Series(s.index, index=s).groupby(level=0).size()
],
labels = ['value_counts', 'Counter', 'np_unique', 'groupby_size'],
n_range = [2 ** k for k in range(5, 25)],
equality_check = lambda *x: (d:= pd.concat(x, axis=1)).eq(d[0], axis=0).all().all(),
xlabel = '~len(s)',
title = f'dtype {datatype}'
)
plot_perfplot('obj')
plot_perfplot('int')