基于条件的自定义标志
我有一个数据集
id | ref | name | conditionCol |
---|---|---|---|
1 | 123 | a | no_error |
1 | 456 | b | error |
1 | 789 | c | no_error |
2 | 231 | d | no_error |
2 | 312 | e | no_error |
2 | 546 | f | no_error |
3 | 645 | g | error |
3 | 879 | h | error |
4 | 789 | i | no_error |
4 | 978 | j | no_error |
I我正在尝试创建一个自定义的error_flag
,条件为:
- 对于每个唯一的
id
列元素 - ,如果
conditionCol
中的任何行具有关键字error
- 甚至
- 如果对于
id
列中的任何元素, - 一行具有关键字
错误
中conditionCol
列,则 - 每一行应在
error_flag
中标记为no
例如,对于 id
:1,全部error_flag
的值为 yes,对于 id 值 1,conditionCol
的第 2 行有 error
id | ref | name | conditionCol | error_flag |
---|---|---|---|---|
1 | 123 | a | no_error | yes |
1 | 456 | b | error | yes |
1 | 789 | c | no_error | yes |
但是,对于 id
:2,error_flag
的所有值为 no,对于 id 值 2,没有行conditionCol
有 error
id | ref | name | conditionCol | error_flag |
---|---|---|---|---|
2 | 231 | d | no_error | no |
2 | 312 | e | no_error | no |
2 | 546 | f | no_error | no |
与 id
值 3 和 3 类似。 4:
id | ref | name | conditionCol | error_flag |
---|---|---|---|---|
3 | 645 | g | no_error | no |
3 | 879 | h | no_error | no |
4 | 789 | i | error | yes |
4 | 978 | j | error | yes |
最终输出为:
id | ref | name | conditionCol | error_flag |
---|---|---|---|---|
1 | 123 | a | no_error | yes |
1 | 456 | b | error | yes |
1 | 789 | c | no_error | 是 |
2 | 231 | d | no_error | 否 |
2 | 312 | e | no_error | no |
2 | 546 | f | no_error | no |
3 | 645 | g | no_error | no |
3 | 879 | h | no_error | no |
4 | 789 | i | error | yes |
4 | 978 | j | error | yes |
更新:
如果你想使用数据集:
import pandas as pd
import numpy as np
id_col = [1,1,1,2,2,2,3,3,4,4]
ref_col = [123,456, 789, 231, 312, 546, 645, 879, 789, 978]
name_col = ['a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
conditionCol = ['no_error', 'error', 'no_error', 'no_error', 'no_error', 'no_error', 'no_error', 'no_error', 'error', 'error']
df = pd.DataFrame(zip(id_col, ref_col, name_col, conditionCol), columns=['id','ref','name','conditionCol'])
df
update2:有没有一种方法可以使用阈值,即:
- 当前问题:每个条件列中至少出现一次关键字
error
所有行,error_flag
中的值为yes
- 单个唯一的
id
,那么对于该 id 值中至少出现 4 次或至少 5 次关键字的 conditionCol 列中的error
具有唯一的id
,那么对于所有行,只有error_flag
中的值为yes
在该 id 值中
I've a dataset
id | ref | name | conditionCol |
---|---|---|---|
1 | 123 | a | no_error |
1 | 456 | b | error |
1 | 789 | c | no_error |
2 | 231 | d | no_error |
2 | 312 | e | no_error |
2 | 546 | f | no_error |
3 | 645 | g | error |
3 | 879 | h | error |
4 | 789 | i | no_error |
4 | 978 | j | no_error |
I'm trying to create a custom error_flag
, condition being:
- for each unique
id
column elements - if any row in the
conditionCol
has the keyworderror
, then - for each row should be flagged as
yes
in theerror_flag
- if for any element in
id
column - not even a single row has the keyword
error
inconditionCol
column, then - for each row should be flagged as
no
in theerror_flag
E.g. For id
:1, all the values of error_flag
is yes, as for id value 1, row #2 of conditionCol
has error
id | ref | name | conditionCol | error_flag |
---|---|---|---|---|
1 | 123 | a | no_error | yes |
1 | 456 | b | error | yes |
1 | 789 | c | no_error | yes |
But, for id
:2, all the values of error_flag
is no, as for id value 2, no row of conditionCol
has error
id | ref | name | conditionCol | error_flag |
---|---|---|---|---|
2 | 231 | d | no_error | no |
2 | 312 | e | no_error | no |
2 | 546 | f | no_error | no |
Similarly for id
value 3 & 4:
id | ref | name | conditionCol | error_flag |
---|---|---|---|---|
3 | 645 | g | no_error | no |
3 | 879 | h | no_error | no |
4 | 789 | i | error | yes |
4 | 978 | j | error | yes |
And final output being:
id | ref | name | conditionCol | error_flag |
---|---|---|---|---|
1 | 123 | a | no_error | yes |
1 | 456 | b | error | yes |
1 | 789 | c | no_error | yes |
2 | 231 | d | no_error | no |
2 | 312 | e | no_error | no |
2 | 546 | f | no_error | no |
3 | 645 | g | no_error | no |
3 | 879 | h | no_error | no |
4 | 789 | i | error | yes |
4 | 978 | j | error | yes |
Update:
If you wish to play around with the dataset:
import pandas as pd
import numpy as np
id_col = [1,1,1,2,2,2,3,3,4,4]
ref_col = [123,456, 789, 231, 312, 546, 645, 879, 789, 978]
name_col = ['a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
conditionCol = ['no_error', 'error', 'no_error', 'no_error', 'no_error', 'no_error', 'no_error', 'no_error', 'error', 'error']
df = pd.DataFrame(zip(id_col, ref_col, name_col, conditionCol), columns=['id','ref','name','conditionCol'])
df
update2: Is there a way to work with thresholds, i.e.:
- current question: atleast one occurrence of keyword
error
in conditionCol column for each individual uniqueid
s, then the value inerror_flag
would beyes
for all the rows in that id value - atleast 4 or atleast 5 occurrence of keyword
error
in conditionCol column for uniqueid
s, then only the value inerror_flag
would beyes
for all the rows in that id value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
numpy.where
与按id
测试每组是否至少有一个值error
:Use
numpy.where
with test if at least one valueerror
per groups byid
: