Search code examples
pythonjupyterexploratory

Most and the least frequent numbers and Outliers - Jupyter


id |car_id
0  |  32
.  |   2
.  |   3
.  |   7
.  |   3
.  |   4
.  |  32
N  |   1

How do you choose the most and the least frequent numbers from the id_car column and present them in a new table with as it often appears?'car_id' and 'quantity'

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


Solution

  • Here's some code that will provide the most frequent ID and the three least-frequent IDs.

    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 has a list of pairs of (ID, count for that ID). It is sorted from most frequent to least frequent. most_common doesn't tell us the order of ties.

    You may change the n to 1 if you only want any of the least-frequent IDs. I made it 3 so that you could see that three tied for least-frequent.