count the frequency that a value occurs in a dataframe column

后端 未结 13 1890
耶瑟儿~
耶瑟儿~ 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:07

    Without any libraries, you could do this instead:

    def to_frequency_table(data):
        frequencytable = {}
        for key in data:
            if key in frequencytable:
                frequencytable[key] += 1
            else:
                frequencytable[key] = 1
        return frequencytable
    

    Example:

    to_frequency_table([1,1,1,1,2,3,4,4])
    >>> {1: 4, 2: 1, 3: 1, 4: 2}
    

提交回复
热议问题