我需要检查pandas数据帧的一列中的字符串是否在另一列中。 示例数据:
aa=['mma', 'sdas', 'asdsad']*1000
t=pd.DataFrame(aa)
a=['m', 'f', 'n']*1000
t1=pd.DataFrame(a)
t2=pd.concat([t,t1], axis=1)
t2.columns=['texto', 'textito']
有了lambda函数,我就得到了我需要的,但是太慢了:
^{pr2}$t2[:10]
texto textito veo1
0 mma m 1
1 sdas f 0
2 asdsad n 0
3 mma m 1
4 sdas f 0
5 asdsad n 0
6 mma m 1
7 sdas f 0
8 asdsad n 0
9 mma m 1
有没有一种更快的方法?在
谢谢。在
使用理解和
zip
更好的回答每个@Ninja Puppy
^{pr2}$更好的答案是每只@Ninja Puppy
按照忍者小狗的建议。使用},这可能不是您想要的。在
set
并检查子集在这种特殊情况下可以使用单个字符串。但是,它也会为'word'
中的'www'
返回{还有
什么时候
定时
特别说明
感谢@Ninja Puppy
请注意,如果我们将理解中的
bool
值赋给pd.Series
,并让一个向量化的操作处理到int
的转换,我们可以节省一些时间。在如果我们导入
contains
操作符并使用python的map
,我们可以获得更高的效率如果空间充足,可以通过将
set
应用于原始数据帧来创建新的数据帧。那么成员资格测试将比对字符串使用in
快得多。在相关问题 更多 >
编程相关推荐