cqc*_*991 12 python numpy matplotlib pandas
我知道我可以通过熊猫绘制直方图:
df4 = pd.DataFrame({'a': np.random.randn(1000) + 1})
df4['a'].hist()
Run Code Online (Sandbox Code Playgroud)
但是如何从这样的情节中检索直方图计数呢?
我知道我可以做到(来自熊猫系列的直方图值)
count,division = np.histogram(df4['a'])
Run Code Online (Sandbox Code Playgroud)
但是在df.hist()使用它之后得到计数值感觉非常多.是否可以直接从熊猫中获取频率值?
piR*_*red 14
快速回答是:
pd.cut(df4['a'], 10).value_counts().sort_index()
Run Code Online (Sandbox Code Playgroud)
从文档:
Run Code Online (Sandbox Code Playgroud)bins: integer, default 10 Number of histogram bins to be used
所以看看 pd.cut(df4['a'], 10).value_counts()
您会看到值与from相同 np.histogram
pd.cut(df4['a'], 10).value_counts().sort_index()
Run Code Online (Sandbox Code Playgroud)
bins: integer, default 10
Number of histogram bins to be used
Run Code Online (Sandbox Code Playgroud)
除格式化和标签外,它看起来都一样.