Are there functions to retrieve the histogram counts of a Series in pandas?

后端 未结 3 1526
故里飘歌
故里飘歌 2021-01-01 14:16

There is a method to plot Series histograms, but is there a function to retrieve the histogram counts to do further calculations on top of it?

I ke

3条回答
  •  暖寄归人
    2021-01-01 14:56

    If your Series was discrete you could use value_counts:

    In [11]: s = pd.Series([1, 1, 2, 1, 2, 2, 3])
    
    In [12]: s.value_counts()
    Out[12]:
    2    3
    1    3
    3    1
    dtype: int64
    

    You can see that s.hist() is essentially equivalent to s.value_counts().plot().

    If it was of floats an awful hacky solution could be to use groupby:

    s.groupby(lambda i: np.floor(2*s[i]) / 2).count()
    

提交回复
热议问题