python - 获取组范围内所有值的计数的有效方法
问题描述
我已经提到了这个问题,稍作修改的解决方案在示例模式下工作,但我的完整数据集内存不足(大约 3GB 的数据)。
我正在尝试查找组范围内所有值的计数(按锚分组):范围公式是y_val +- (anchor_val / 20)
. 请注意,anchor_val
所有锚点都是一致的,例如:
ID | 锚 | y_val | 锚定值 |
---|---|---|---|
12 | 抗体 | 80 | 40 |
13 | 抗体 | 20 | 40 |
14 | 美国广播公司 | 80 | 50 |
15 | 美国广播公司 | 80 | 50 |
16 | 抗体 | 81 | 40 |
17 | abd | 80 | 50 |
这将导致:
ID | 锚 | y_val | 锚定值 | (anchor_val / 20) | 数数 |
---|---|---|---|---|---|
12 | 抗体 | 80 | 40 | 2 | 1 |
13 | 抗体 | 20 | 40 | 2 | 0 |
14 | 美国广播公司 | 80 | 50 | 2.5 | 1 |
15 | 美国广播公司 | 79 | 50 | 2.5 | 1 |
16 | 抗体 | 81 | 40 | 2 | 1 |
17 | abd | 80 | 50 | 2.5 | 0 |
(为了清楚起见,我添加了 anchor_val/20)。
编辑:
导致内存不足错误的当前代码:
df["rule_8_comp_low"] = df["y_val"] - df["anchor_val"] / 20
df["rule_8_comp_high"] = df["y_val"] + df["anchor_val"] / 20
m = df.reset_index().merge(
df[["anchor_col", "y_val"]].reset_index(), on="anchor_col"
)
m["rule_8_to_count"] = (
m.y_val_y.ge(m.rule_8_comp_low)
& m.y_val_y.le(m.rule_8_comp_high)
& (m.index_x != m.index_y)
)
df["y_val_between"] = m.groupby("index_x").rule_8_to_count.sum()
解决方案
推荐阅读
- oracle - jdbc的sql脚本中连接用户名/密码@sid的选项是什么
- java - 在 Fragment 中返回后如何通过 URL 更改图像颜色
- r - R ggplot:如何格式化x轴以使第一个标签与后续标签不同
- firefox-addon - 无法与本机消息传递主机 (firefox) 通信
- jquery - 尝试使用 jQuery 预加载网页时出现“TypeError: e is null”
- python - 如何使用 Python 执行 SSIS ETL 包
- python - 使用“if in”时元素现有标识错误
- c# - 从 Microsoft Store 将 Edge Extensions 集成到 Webview UWP
- java - 给出 0 到 15 之间的值,返回 4 个 CheckBoxes 之间的正确选择
- xml - 查找并替换为星号字符