python - Pandas - 根据多个条件更新列 - 按方法分组
问题描述
我的目标是以下输出。
一个 | 乙 | C | D | 乙 | F |
---|---|---|---|---|---|
0000 | ZZZ | 987 | QW1 | 8 | 前三四列和偏移量 |
0000 | ZZZ | 987 | QW1 | -8 | 前三四列和偏移量 |
0000 | ZZZ | 987 | QW1 | -8 | 第一个或没有匹配 |
1111 | AAA | 123 | AB1 | 1 | 前三四列和偏移量 |
1111 | AAA | 123 | CD1 | -1 | 前三四列和偏移量 |
2222 | BBB | 456 | EF1 | -4 | 前三四列和偏移量 |
2222 | BBB | 456 | GH1 | -1 | 前三四列和偏移量 |
2222 | BBB | 456 | IL1 | 5 | 前三四列和偏移量 |
3333 | CCC | 789 | MN1 | 2 | 前两个 col 和 offset |
3333 | CCC | 101 | MN1 | -2 | 前两个 col 和 offset |
4444 | DDD | 121 | UYT | 6 | 前两个 col 和 offset |
4444 | DDD | 131 | FB1 | -5 | 前两个 col 和 offset |
4444 | DDD | 141 | UYT | -1 | 前两个 col 和 offset |
5555 | 电子电气设备 | 151 | CB1 | 3 | 前两个 col 和 offset |
5555 | 电子电气设备 | 161 | CR1 | -3 | 前两个 col 和 offset |
5555 | 电子电气设备 | 161 | CR1 | -5 | 第一个或没有匹配 |
6666 | FFF | 111 | CB1 | 4 | 第一个或没有匹配 |
7777 | GGG | 222 | ZB1 | 10.5 | 前三四列和小偏移 |
7777 | GGG | 222 | ZB1 | -10 | 前三四列和小偏移 |
第一条规则)前三列必须彼此相等 - 无论第四列如何,它可以相等或不相等。每个组合必须将关联的数字 (col E) 偏移为零(可以从 2 到 X 记录组合)。即使条目不均匀,它也应该有效。
第二条规则)前两列必须彼此相等 - 无论第四列如何,它可以相等或不相等。每个组合必须将关联的数字 (col E) 偏移为零(可以从 2 到 X 记录组合)。即使条目不均匀,它也应该有效。
第三条规则)不匹配。
第 4 条规则)前三列必须彼此相等 - 无论第四列如何,它可以相等或不相等。每个组合可以有0.5
AT MOST (col E) 和 NO 偏移为零的差异(可以从 2 到 X 记录组合)。即使条目不均匀,它也应该有效。
请在下面查看我的代码。
通过下面的代码,我能够得到类似的结果,但是,这种方式不适用于不均匀的条目,例如前三行将导致如下:
一个 | 乙 | C | D | 乙 | F |
---|---|---|---|---|---|
0000 | ZZZ | 987 | QW1 | 8 | 第一个或没有匹配 |
0000 | ZZZ | 987 | QW1 | -8 | 第一个或没有匹配 |
0000 | ZZZ | 987 | QW1 | -8 | 第一个或没有匹配 |
而不是以下内容:
一个 | 乙 | C | D | 乙 | F |
---|---|---|---|---|---|
0000 | ZZZ | 987 | QW1 | 8 | 前三四列和偏移量 |
0000 | ZZZ | 987 | QW1 | -8 | 前三四列和偏移量 |
0000 | ZZZ | 987 | QW1 | -8 | 第一个或没有匹配 |
到目前为止的代码:
m1 = df.groupby(['A', 'B', 'C'])['E'].transform('sum').eq(0) # Rule 1
m2 = df.groupby(['A', 'B'])['E'].transform('sum').eq(0) # Rule 2
m3 = df.groupby(['A', 'B', 'C'])['E'].transform('sum').abs().le(0.5) # Rule 4
df['new'] = np.select([m1, m2, m3], ['first three-four col and offset',
'first two col and offset', 'first three-four col and small offset'], 'first or no match')
解决方案
这是解决方案的一个潜在开始......我怀疑这个逻辑需要更加强大才能处理您的真实世界数据集。
#Read in your dataframe from this question
df = pd.read_clipboard(dtype={'A':'str'})
def f2(x):
cum = x.cumsum()
m = (cum == 0)[::-1].cumsum()[::-1].astype(bool)
x[m]='first two col and offset'
x[~m]=np.nan
return x
def f1(x):
cum = x.cumsum()
m = (cum == 0)[::-1].cumsum()[::-1].astype(bool)
x[m]='first three col and offset'
cl = ((cum.abs() <= .5) & (cum != 0))[::-1].cumsum()[::-1].astype(bool)
x[cl] = 'first three col and small offset'
x[~m & ~cl] = np.nan
return x
df['F2'] = df.groupby(['A','B'])['E'].apply(f2)
df['F1'] = df.groupby(['A', 'B', 'C'])['E'].apply(f1)
df['F'] = df['F1'].fillna(df['F2']).fillna('first or no match')
df = df.drop(['F1', 'F2'], axis=1)
输出:
A B C D E F
0 0000 ZZZ 987 QW1 8.0 first three col and offset
1 0000 ZZZ 987 QW1 -8.0 first three col and offset
2 0000 ZZZ 987 QW1 -8.0 first or no match
3 1111 AAA 123 AB1 1.0 first three col and offset
4 1111 AAA 123 CD1 -1.0 first three col and offset
5 2222 BBB 456 EF1 -4.0 first three col and offset
6 2222 BBB 456 GH1 -1.0 first three col and offset
7 2222 BBB 456 IL1 5.0 first three col and offset
8 3333 CCC 789 MN1 2.0 first two col and offset
9 3333 CCC 101 MN1 -2.0 first two col and offset
10 4444 DDD 121 UYT 6.0 first two col and offset
11 4444 DDD 131 FB1 -5.0 first two col and offset
12 4444 DDD 141 UYT -1.0 first two col and offset
13 5555 EEE 151 CB1 3.0 first two col and offset
14 5555 EEE 161 CR1 -3.0 first two col and offset
15 5555 EEE 161 CR1 -5.0 first or no match
16 6666 FFF 111 CB1 4.0 first or no match
17 7777 GGG 222 ZB1 10.5 first three col and small offset
18 7777 GGG 222 ZB1 -10.0 first three col and small offset
细节:
f2,基于相同的“A”和“B”获取一组“E”,然后计算累积和。
然后我们检查 cumsum 等于 0 的位置以创建一个布尔系列。
反转,使用 [::-1] 以负的一步切片,该系列并再次使用 cumsum 标记 cumsum == 0 之前的所有记录。
使用 [::-1] 恢复到原始顺序并强制转换为布尔值。
接下来,使用该布尔系列的真值来设置“前两个列和偏移量”,然后使用假记录来设置 np.nan。
f1,除了使用额外的逻辑来捕获偏移关闭记录外,其他操作相同。
推荐阅读
- javascript - 如何使用输入类型文本创建 html 代码(或其他代码)
- javascript - 带有弹出窗口的(多边形形状)图层顶部带有弹出窗口的 Mapbox 标记显示 2 个弹出窗口
- javascript - 非常慢的交互与大量数据负载
- javascript - 比较Mongo中的两个参考?
- python - Python调用类函数而不引用类名
- python - 格式化 Python 列表
- javascript - 在反应js中将经纬度作为道具传递的最佳方法
- c# - 如何在 .NET 中表示 URN(统一资源名称),以便 Equals 按预期工作
- php - laravel 中的唯一验证
- javascript - 对表执行计数