1

假设我们有一个向量大小N=1000,假设我们得到了列表[1,1,2,2,2,100]

我想生成一个大小为 1000 的 np.array (或 pd.Series),其中v[n]是列表中出现的次数n。在我们的示例中,v[1] = 2, v[2] = 3, v[100] = 1, v=[42] = 0

我怎样才能优雅地用 numpy/pandas 做到这一点?

4

5 回答 5

3

如果你有一个列表mylist,你可以得到一个计数数组mycount

N = 1000
x = np.array(mylist)
mycount = np.bincount(x, minlength=N)

这会将数组中的每个元素根据其值和数量分类到 bin 中。bincount您可以在此文档页面上找到更多信息。

于 2020-12-08T13:56:07.690 回答
2

Python 有一个用于计算发生次数的本机方法,Counter该方法可以在不调用numpypandas需要时使用

from collections import Counter
a = [1,1,2,2,2,100]
cnts = Counter(a)
print(cnts)
# Counter({2: 3, 1: 2, 100: 1})

您可以将其转换为具有列表理解的列表:

N = 100
cnts_list = [cnts.get(i, 0) for i in range(N+1)]
于 2020-12-08T14:00:29.947 回答
1

使用Series.value_countswithSeries.reindex添加不存在的值:

a = [1,1,2,2,2,100]

N = 100
a = pd.Series(a).value_counts().reindex(range(N+1), fill_value=0)
print (a)
0      0
1      2
2      3
3      0
4      0
      ..
96     0
97     0
98     0
99     0
100    1
Length: 101, dtype: int64
于 2020-12-08T13:54:17.137 回答
1

你也可以使用np.unique

N = 1000
result = np.zeros(N)
idx, val = np.unique([1,1,2,2,2,100], return_counts=True)
result[idx] = val
print(result[:5])                                                                                                                                                                                                                                                           
>>>[0. 2. 3. 0. 0.]

更多信息:https ://numpy.org/doc/stable/reference/generated/numpy.unique.html

于 2020-12-08T14:17:13.520 回答
0

您可以使用系列和分组方式

In[1]:

import pandas as pd
my_list = [1,1,1,2,2,2,2,3,4,8,1000,8,8,5,5,6]

my_Serie = pd.Series(my_list)
v = my_Serie.groupby(my_list).count().to_dict()
print(v)

{1: 3, 2: 4, 3: 1, 4: 1, 5: 2, 6: 1, 8: 3, 1000: 1}
于 2020-12-08T14:24:06.070 回答