python-3.x 如果多列同时包含多个字符,则返回True

0qx6xfy6  于 2023-02-26  发布在  Python
关注(0)|答案(2)|浏览(153)

对于以下数据df

id               k1            k2
0   1         re_setup      oo_setup
1   2         oo_setup      oo_setup
2   3         alerting        bounce
3   4           bounce  re_oversetup
4   5     re_oversetup      alerting
5   6       alerting_s      re_setup
6   7     re_oversetup      oo_setup
7   8         alerting        bounce
8   9  alerting_bounce        bounce

我们希望:如果K1K2列包含字符setupbounce,则返回True。否则,返回False。请注意,如果K1包含setupK2包含bounce,或者 * 反之亦然 *,则这种情况下返回False
如何实现呢?谢谢。
预期成果如下:

id               k1               k2   same
0   1         re_setup         oo_setup   True
1   2         oo_setup         oo_setup   True
2   3         alerting           bounce  False
3   4           bounce     re_oversetup  False
4   5     re_oversetup  alerting_bounce  False
5   6       alerting_s         re_setup  False
6   7     re_oversetup         oo_setup   True
7   8         alerting           bounce  False
8   9  alerting_bounce           bounce   True

我尝试使用df['same1'] = df[['k1', 'k2']].apply(lambda x: x.str.contains('setup|bounce')).all(1),它返回以下结果:

id               k1               k2   same  same1
0   1         re_setup         oo_setup   True   True
1   2         oo_setup         oo_setup   True   True
2   3         alerting           bounce  False  False
3   4           bounce     re_oversetup  False   True  incorrect result
4   5     re_oversetup  alerting_bounce  False   True  incorrect result
5   6       alerting_s         re_setup  False  False
6   7     re_oversetup         oo_setup   True   True
7   8         alerting           bounce  False  False
8   9  alerting_bounce           bounce   True   True

我们可以看到第3行和第4行返回了错误的结果。

    • 参考:**

If one row in two columns contain the same string python pandas

iswrvxsc

iswrvxsc1#

使用str.extract并比较结果:

s1 = df['k1'].str.extract('(setup|bounce)', expand=False)
s2 = df['k2'].str.extract('(setup|bounce)', expand=False)

df['same'] = s1.eq(s2)

输出:

id               k1            k2   same
0   1         re_setup      oo_setup   True
1   2         oo_setup      oo_setup   True
2   3         alerting        bounce  False
3   4           bounce  re_oversetup  False
4   5     re_oversetup      alerting  False
5   6       alerting_s      re_setup  False
6   7     re_oversetup      oo_setup   True
7   8         alerting        bounce  False
8   9  alerting_bounce        bounce   True

所有匹配项

s1 = df['k1'].str.extractall('(setup|bounce)')[0].groupby(level=0).agg(set)
s2 = df['k2'].str.extractall('(setup|bounce)')[0].groupby(level=0).agg(set)
df['same_all'] = s1.eq(s2)

输出:

id               k1            k2  same_all
0   1         re_setup      oo_setup      True
1  2a         oo_setup  bounce_setup     False  # only 1 match
2  2b     setup_bounce  bounce_setup      True  # all matches
3   3         alerting        bounce     False
4   4           bounce  re_oversetup     False
5   5     re_oversetup      alerting     False
6   6       alerting_s      re_setup     False
7   7     re_oversetup      oo_setup      True
8   8         alerting        bounce     False
9   9  alerting_bounce        bounce      True
ruarlubt

ruarlubt2#

您可以使用、更改列名和字符串:

def func1(x):
    if 'df' in x['a'] and 'ww' in x['b']:
        return 'True   '
    else:
        return 'False'

df['status'] = df.apply(func1, axis=1)

相关问题