id |car_id
0 | 32
. | 2
. | 3
. | 7
. | 3
. | 4
. | 32
N | 1
您如何从 id_car 列中选择最频繁和最不频繁的数字,并将它们显示在一个新表中,就像它经常出现的那样?'car_id' 和 'quantity'
mdata['car_id'].value_counts().idxmax()
id |car_id
0 | 32
. | 2
. | 3
. | 7
. | 3
. | 4
. | 32
N | 1
您如何从 id_car 列中选择最频繁和最不频繁的数字,并将它们显示在一个新表中,就像它经常出现的那样?'car_id' 和 'quantity'
mdata['car_id'].value_counts().idxmax()
下面是一些代码,它们将提供最频繁的 ID 和三个最不频繁的 ID。
from collections import Counter
car_ids = [32, 2, 3, 7, 3, 4, 32, 1]
c = Counter(car_ids)
count_pairs = c.most_common() # Gets all counts, from highest to lowest.
print (f'Most frequent: {count_pairs[0]}') # Most frequent: (32, 2)
n = 3
print (f'Least frequent {n}: {count_pairs[:-n-1:-1]}') # Least frequent 3: [(1, 1), (4, 1), (7, 1)]
count_pairs
有一个 (ID, count for that ID ) 对的列表。它从最频繁到最不频繁排序。most_common
没有告诉我们关系的顺序。
如果您只需要任何频率最低的 ID,则可以将 n 更改为 1。我将其设为 3,以便您可以看到这三个并列的频率最低。