列包含第4列 [英] Column contains column 4

查看:61
本文介绍了列包含第4列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框.我想测试(C)在每一行中(B)列中的数字是否在字符串(A)列中.

I have a dataframe. I would like to test whether, (C), on each row, the number in column (B) is in the string, column (A).

df = pd.DataFrame({'A': ["me 123", "me-123", "1234", "me 12", "123 me", "6 you 123-me"],
                   'B': [123,       123,      123,    123,     6,        123]})

我几乎可以使用提取功能做到这一点

I can almost do that using extract

df['C'] = df.A.str.extract('(\d+)', expand=False).astype(float).eq(df.B,0).astype(int)

              A    B  C
0        me 123  123  1
1        me-123  123  1
2          1234  123  0
3         me 12  123  0
4        123 me    6  0
5  6 you 123-me  123  0

但是在最下面的一行中却没有看到数字123,因为它是数字6.我想得到

However on the bottom row it is not seeing the number 123 becasue of the number 6. I would like to get

              A    B  C
0        me 123  123  1
1        me-123  123  1
2          1234  123  0
3         me 12  123  0
4        123 me    6  0
5  6 you 123-me  123  1

推荐答案

使用findall

[y in x for x , y in zip(df.A.str.findall('(\d+)'),df.B.astype(str))]
Out[733]: [True, True, False, False, False, True]

这篇关于列包含第4列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆