Search code examples
pythonpandashistogrampandas-groupbyfrequency-distribution

Can't obtain histogram from pandas grouped data


I have this DF.

f = { 'Router_name':['count'] }
a = a.groupby(['Week_end']).agg(f)

Which produces the following data ..

         Router_name
               count
Week_end            
29                 3
30                10
31                 6
32                 4
33                 9
34                 2
35                 5
36                10
37                 8
38                 6
40                10
41                 2
42                 8
43                 1
44                 3
45                 2
46                 8
47                 6
49                12
50                 5
51                10
52                 5
53                11

I'm trying to obtain an histogram/frequencies out of the previous aggregated data for Router_name. So, for example, the expected output should these frequencies:

enter image description here

I've read here that doing b = a.hist(by=a['Router_name']) would do the trick. However, when trying that I get the following error:

Traceback (most recent call last):
  File "get_report_v1.5_devel.py", line 465, in <module>
    b = a.hist(by=a['Router_name'])
    raise ValueError("Grouper for '%s' not 1-dimensional" % t)
ValueError: Grouper for '<class 'pandas.core.frame.DataFrame'>' not 1-dimensional

I also tried this: a.Router_name.hist(). But I get the same DataFrame.

How can I obtain the frequencies for a given column out of a grouped data?


Solution

  • By seems like you create the multi index by using the agg

    a.hist(by=a[('Router_name','count')])
    
    a[('Router_name','count')].value_counts()
    Out[1678]: 
    10    4
    8     3
    6     3
    5     3
    2     3
    3     2
    12    1
    11    1
    9     1
    4     1
    1     1
    Name: (Router_name, sum), dtype: int64