count the frequency that a value occurs in a dataframe column

后端 未结 13 2051
耶瑟儿~
耶瑟儿~ 2020-11-22 03:29

I have a dataset

|category|
cat a
cat b
cat a

I\'d like to be able to return something like (showing unique values and frequency)



        
13条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 04:16

    @metatoaster has already pointed this out. Go for Counter. It's blazing fast.

    import pandas as pd
    from collections import Counter
    import timeit
    import numpy as np
    
    df = pd.DataFrame(np.random.randint(1, 10000, (100, 2)), columns=["NumA", "NumB"])
    

    Timers

    %timeit -n 10000 df['NumA'].value_counts()
    # 10000 loops, best of 3: 715 µs per loop
    
    %timeit -n 10000 df['NumA'].value_counts().to_dict()
    # 10000 loops, best of 3: 796 µs per loop
    
    %timeit -n 10000 Counter(df['NumA'])
    # 10000 loops, best of 3: 74 µs per loop
    
    %timeit -n 10000 df.groupby(['NumA']).count()
    # 10000 loops, best of 3: 1.29 ms per loop
    

    Cheers!

提交回复
热议问题