频率和频率最低的数字和异常值-Jupyter

问题描述 投票:0回答:1
id |car_id
0  |  32
.  |   2
.  |   3
.  |   7
.  |   3
.  |   4
.  |  32
N  |   1

您如何从id_car列中选择频率最高和频率最低的数字,并将其以新出现的形式显示在新表中?'car_id'和'quantity'

mdata['car_id'].value_counts().idxmax()

python jupyter exploratory
1个回答
0
投票

这里有一些代码将提供最频繁的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,该ID的计数)。从最频繁到最不频繁进行排序。 most_common不会告诉我们领带的顺序。

如果您只想要任何频率最低的ID,可以将n更改为1。我把它设为3,这样您就可以看到最少的并列3。

© www.soinside.com 2019 - 2024. All rights reserved.