我很好奇是否可以计算直方图中特定bin的元素数量,即0-10范围内的所有元素
您将如何做?
例如plt.hist(data,bins = [0,10,20,30,40,50,100]) 可以对数据集中进入bin 0-10的所有元素进行计数
答案 0 :(得分:3)
Matplotlib直方图返回每个bin的计数:
import matplotlib.pyplot as plt
import numpy as np
x = np.random.uniform(0, 100, 1000)
counts, edges, plot = plt.hist(x, bins=[0, 10, 20, 50, 100])
print(counts)
print(counts[0]) # first bin
答案 1 :(得分:0)
是的,pd.Series.value_counts
具有bins
参数。
import pandas as pd
s = pd.Series(np.random.randint(0,100,50))
s.value_counts(bins=[0,10,20,30,40,50,60,70,80,90,100]).sort_index()
输出:
(-0.001, 10.0] 8
(10.0, 20.0] 6
(20.0, 30.0] 5
(30.0, 40.0] 6
(40.0, 50.0] 2
(50.0, 60.0] 3
(60.0, 70.0] 4
(70.0, 80.0] 3
(80.0, 90.0] 6
(90.0, 100.0] 7
dtype: int64