我有一个pandas数据框架。
Action_flag不交易 不交易不交易不交易不交易不交易不交易
IIUC,您可以使用pd.cut
创建箱子,然后使用crosstab
获得每个动作标志的计数。最后,assign
将逐行汇总到一个新列"Grand_total":
out = pd.crosstab(pd.cut(df['Score'], np.linspace(0,1,21), include_lowest=True), df['Action_flag']).assign(Grand_total=lambda x: x.sum(axis=1))
输出:
Action_flag Not Traded Traded Grand_total
Score
(-0.001, 0.05] 0 3 3
(0.3, 0.35] 1 0 1
(0.4, 0.45] 1 0 1
(0.45, 0.5] 1 0 1
(0.55, 0.6] 1 0 1
(0.6, 0.65] 1 1 2
(0.75, 0.8] 1 0 1
(0.95, 1.0] 1 0 1