Frequency table for a single variable

后端 未结 4 1499
温柔的废话
温柔的废话 2020-11-29 19:54

One last newbie pandas question for the day: How do I generate a table for a single Series?

For example:

my_series = pandas.Series([1,2,2,3,3,3])
pa         


        
4条回答
  •  误落风尘
    2020-11-29 20:08

    Maybe .value_counts()?

    >>> import pandas
    >>> my_series = pandas.Series([1,2,2,3,3,3, "fred", 1.8, 1.8])
    >>> my_series
    0       1
    1       2
    2       2
    3       3
    4       3
    5       3
    6    fred
    7     1.8
    8     1.8
    >>> counts = my_series.value_counts()
    >>> counts
    3       3
    2       2
    1.8     2
    fred    1
    1       1
    >>> len(counts)
    5
    >>> sum(counts)
    9
    >>> counts["fred"]
    1
    >>> dict(counts)
    {1.8: 2, 2: 2, 3: 3, 1: 1, 'fred': 1}
    

提交回复
热议问题