基于条件的自定义标志

发布于 2025-01-15 14:43:07 字数 4681 浏览 2 评论 0原文

我有一个数据集


idrefnameconditionCol
1123ano_error
1456berror
1789cno_error
2231dno_error
2312eno_error
2546fno_error
3645gerror
3879herror
4789ino_error
4978jno_error

I我正在尝试创建一个自定义的error_flag,条件为:

  • 对于每个唯一的id列元素
  • ,如果conditionCol中的任何行具有关键字error
  • 甚至
  • 如果对于 id 列中的任何元素,
  • 一行具有关键字 错误conditionCol 列,则
  • 每一行应在 error_flag 中标记为 no

例如,对于 id:1,全部error_flag 的值为 yes,对于 id 值 1,conditionCol 的第 2 行有 error


idrefnameconditionColerror_flag
1123ano_erroryes
1456berroryes
1789cno_erroryes

但是,对于 id:2,error_flag 的所有值为 no,对于 id 值 2,没有行conditionColerror


idrefnameconditionColerror_flag
2231dno_errorno
2312eno_errorno
2546fno_errorno

id 值 3 和 3 类似。 4:


idrefnameconditionColerror_flag
3645gno_errorno
3879hno_errorno
4789ierroryes
4978jerroryes

最终输出为:


idrefnameconditionColerror_flag
1123ano_erroryes
1456berroryes
1789cno_error
2231dno_error
2312eno_errorno
2546fno_errorno
3645gno_errorno
3879hno_errorno
4789ierroryes
4978jerroryes

更新


如果你想使用数据集:


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

idrefnameconditionCol
1123ano_error
1456berror
1789cno_error
2231dno_error
2312eno_error
2546fno_error
3645gerror
3879herror
4789ino_error
4978jno_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 keyword error, then
  • for each row should be flagged as yes in the error_flag
  • if for any element in id column
  • not even a single row has the keyword error in conditionCol column, then
  • for each row should be flagged as no in the error_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

idrefnameconditionColerror_flag
1123ano_erroryes
1456berroryes
1789cno_erroryes

But, for id:2, all the values of error_flag is no, as for id value 2, no row of conditionCol has error

idrefnameconditionColerror_flag
2231dno_errorno
2312eno_errorno
2546fno_errorno

Similarly for id value 3 & 4:

idrefnameconditionColerror_flag
3645gno_errorno
3879hno_errorno
4789ierroryes
4978jerroryes

And final output being:

idrefnameconditionColerror_flag
1123ano_erroryes
1456berroryes
1789cno_erroryes
2231dno_errorno
2312eno_errorno
2546fno_errorno
3645gno_errorno
3879hno_errorno
4789ierroryes
4978jerroryes

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 unique ids, then the value in error_flag would be yes for all the rows in that id value
  • atleast 4 or atleast 5 occurrence of keyword error in conditionCol column for unique ids, then only the value in error_flag would be yes for all the rows in that id value

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪眸﹌ 2025-01-22 14:43:07

使用 numpy.where 与按 id 测试每组是否至少有一个值 error

m = df['id'].isin(df.loc[df['conditionCol'].eq('error'), 'id'])
#alternative
#m = df['conditionCol'].eq('error').groupby(df['id']).transform('any')
df['error_flag'] = np.where(m, 'yes', 'no')

print (df)
   id  ref name conditionCol error_flag
0   1  123    a     no_error        yes
1   1  456    b        error        yes
2   1  789    c     no_error        yes
3   2  231    d     no_error         no
4   2  312    e     no_error         no
5   2  546    f     no_error         no
6   3  645    g     no_error         no
7   3  879    h     no_error         no
8   4  789    i        error        yes
9   4  978    j        error        yes

Use numpy.where with test if at least one value error per groups by id:

m = df['id'].isin(df.loc[df['conditionCol'].eq('error'), 'id'])
#alternative
#m = df['conditionCol'].eq('error').groupby(df['id']).transform('any')
df['error_flag'] = np.where(m, 'yes', 'no')

print (df)
   id  ref name conditionCol error_flag
0   1  123    a     no_error        yes
1   1  456    b        error        yes
2   1  789    c     no_error        yes
3   2  231    d     no_error         no
4   2  312    e     no_error         no
5   2  546    f     no_error         no
6   3  645    g     no_error         no
7   3  879    h     no_error         no
8   4  789    i        error        yes
9   4  978    j        error        yes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文