python - Pandas 中的直方图

标签 python pandas numpy histogram bins

对于 python 和 pandas 来说相对较新。我有一个数据框:df,其中有 2 列(例如 01)和 n 行。我想绘制两列中表示的两个时间序列数据的直方图。我还需要访问每个箱的直方图中的确切计数,以便以后进行操作。

b_counts, b_bins = np.histogram(df[0], bins = 10)
a_counts, a_bins = np.histogram(df[1], bins = 10)

plt.bar(b_bins, b_counts)
plt.pbar(a_bins, a_counts)

但是,我收到大小不兼容的错误,即 bins 数组的长度为 11,而 counts 数组的长度为 10。两个问题: 1)为什么numpy中的直方图有一个额外的bin?即 11 个垃圾箱而不是 10 个垃圾箱 2)假设上面的问题1)可以解决,这是解决这个问题的最好/最简单的方法吗?

最佳答案

我会直接使用 Pyplot 内置的 histogram功能:

b_counts, b_bins, _ = plt.hist(df[0], bins = 10)
a_counts, a_bins, _ = plt.hist(df[1], bins = 10)
<小时/>

根据 numpy.histogram 的文档(如果您向下滚动足够远以阅读参数定义中的 Returns 部分):

hist : array The values of the histogram. See density and weights for a description of the possible semantics.

bin_edges : array of dtype float Return the bin edges (length(hist)+1).

很清楚,不是吗?

关于python - Pandas 中的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39628242/

相关文章:

python - 如何在单个 np.where 条件中使用多个值?

python - 如何在GimpFU中传递图像路径

python - 在 Python 中的列表理解中的两个列表之间进行 bool 检查的快速方法?

python - 压平并展开 csv 文件?

python - Airflow 网络服务器命令失败,{filesystemcache.py :224} ERROR - Operation not permitted

python - 如何将函数的返回值写入 pandas 数据帧的新列

python - 未绑定(bind)本地错误: local variable 'y' referenced before assignment in Pandas

python - Pandas 数据帧 : groupby then transpose

python - numpy 中带有数组索引的嵌套循环

python - 假设 0 * infinity = 0,如何在 NumPy 中乘法 outer()?