【发布时间】:2016-08-18 10:18:30
【问题描述】:
我是 Pandas 的新手,请不要太苛刻 ;) 假设我的初始数据框如下所示:
#::: initialize dictionary
np.random.seed(0)
d = {}
d['size'] = 2 * np.random.randn(100) + 3
d['flag_A'] = np.random.randint(0,2,100).astype(bool)
d['flag_B'] = np.random.randint(0,2,100).astype(bool)
d['flag_C'] = np.random.randint(0,2,100).astype(bool)
#::: convert dictionary into pandas dataframe
df = pd.DataFrame(d)
我现在根据“大小”对数据框进行分箱,
#::: bin pandas dataframe per size
bins = np.arange(0,10,1)
groups = df.groupby( pd.cut( df['size'], bins ) )
导致此输出:
---
(0, 1]
flag_A flag_B flag_C size
25 False False True 0.091269
40 True True True 0.902894
41 True True True 0.159964
46 False True True 0.494409
53 False True True 0.638736
73 True False True 0.530348
80 True False False 0.669700
88 True True True 0.858495
---
(1, 2]
flag_A flag_B flag_C size
...
我现在的问题是:如何从这里开始计算每个垃圾箱中每个标志(A、B、C)的真假计数?例如。对于 bin=(0,1],我希望得到类似 N_flag_A_true = 5、N_flag_A_false = 3 等的信息。理想情况下,我希望通过扩展此数据帧或将这些信息汇总到一个新数据帧中。
【问题讨论】:
标签: python pandas count histogram bin