pandas -检查一个数据帧中的字符串列是否包含来自另一个数据帧的一对字符串 [英] Pandas - check if a string column in one dataframe contains a pair of strings from another dataframe

查看:129
本文介绍了 pandas -检查一个数据帧中的字符串列是否包含来自另一个数据帧的一对字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是基于我问的另一个问题,我没有完全解决这个问题:

This question is based on another question I asked, where I didn't cover the problem entirely: Pandas - check if a string column contains a pair of strings

这是问题的修改版本.

This is a modified version of the question.

我有两个数据框:

df1 = pd.DataFrame({'consumption':['squirrel ate apple', 'monkey likes apple', 
                                  'monkey banana gets', 'badger gets banana', 'giraffe eats grass', 'badger apple loves', 'elephant is huge', 'elephant eats banana tree', 'squirrel digs in grass']})

df2 = pd.DataFrame({'food':['apple', 'apple', 'banana', 'banana'], 
                   'creature':['squirrel', 'badger', 'monkey', 'elephant']})

目标是测试df1.consumptions中是否存在df.food:df.creature对.

The goal is to test if df.food:df.creature pairs are present in df1.consumptions.

在上面的示例中,此测试的预期答案为:

The expected answer for this test in the above example would be :

['True', 'False', 'True', 'False', 'False', 'True', 'False', 'True', 'False']

模式是:

松鼠吃了苹果= True,因为松鼠和苹果是一对. 猴子喜欢apple = False,因为我们不要找猴子和苹果.

squirrel ate apple = True since squirrel and apple is a pair. monkey likes apple = False since monkey and apple is not a pair we are looking for.

我当时正在考虑构建一个具有成对值的数据帧的字典,其中每个数据帧都将用于一个生物,例如松鼠,猴子等,然后使用np.where创建一个布尔表达式并执行一个str.contains.

I was thinking of constructing a dictionary of dataframes of the pair-values where each dataframe would be for one creature for e.g.squirrel, monkey etc. and then using np.where to create a boolean expression and perform a str.contains.

不确定这是否是最简单的方法.

Not sure if that is the easiest way.

推荐答案

请考虑以下矢量化方法:

Consider this vectorized approach:

from sklearn.feature_extraction.text import CountVectorizer

vect = CountVectorizer()

X = vect.fit_transform(df1.consumption)
Y = vect.transform(df2.creature + ' ' + df2.food)

res = np.ravel(np.any((X.dot(Y.T) > 1).todense(), axis=1))

结果:

In [67]: res
Out[67]: array([ True, False,  True, False, False,  True, False,  True, False], dtype=bool)

说明:

In [68]: pd.DataFrame(X.toarray(), columns=vect.get_feature_names())
Out[68]:
   apple  ate  badger  banana  digs  eats  elephant  gets  giraffe  grass  huge  in  is  likes  loves  monkey  squirrel  tree
0      1    1       0       0     0     0         0     0        0      0     0   0   0      0      0       0         1     0
1      1    0       0       0     0     0         0     0        0      0     0   0   0      1      0       1         0     0
2      0    0       0       1     0     0         0     1        0      0     0   0   0      0      0       1         0     0
3      0    0       1       1     0     0         0     1        0      0     0   0   0      0      0       0         0     0
4      0    0       0       0     0     1         0     0        1      1     0   0   0      0      0       0         0     0
5      1    0       1       0     0     0         0     0        0      0     0   0   0      0      1       0         0     0
6      0    0       0       0     0     0         1     0        0      0     1   0   1      0      0       0         0     0
7      0    0       0       1     0     1         1     0        0      0     0   0   0      0      0       0         0     1
8      0    0       0       0     1     0         0     0        0      1     0   1   0      0      0       0         1     0

In [69]: pd.DataFrame(Y.toarray(), columns=vect.get_feature_names())
Out[69]:
   apple  ate  badger  banana  digs  eats  elephant  gets  giraffe  grass  huge  in  is  likes  loves  monkey  squirrel  tree
0      1    0       0       0     0     0         0     0        0      0     0   0   0      0      0       0         1     0
1      1    0       1       0     0     0         0     0        0      0     0   0   0      0      0       0         0     0
2      0    0       0       1     0     0         0     0        0      0     0   0   0      0      0       1         0     0
3      0    0       0       1     0     0         1     0        0      0     0   0   0      0      0       0         0     0

更新:

In [92]: df1['match'] = np.ravel(np.any((X.dot(Y.T) > 1).todense(), axis=1))

In [93]: df1
Out[93]:
                 consumption  match
0         squirrel ate apple   True
1         monkey likes apple  False
2         monkey banana gets   True
3         badger gets banana  False
4         giraffe eats grass  False
5         badger apple loves   True
6           elephant is huge  False
7  elephant eats banana tree   True
8     squirrel digs in grass  False
9        squirrel.eats/apple   True   # <----- NOTE

这篇关于 pandas -检查一个数据帧中的字符串列是否包含来自另一个数据帧的一对字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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