使用python搜索数据框的另一列或另一个数据框中是否存在任何单词 [英] searching if anyone of word is present in the another column of a dataframe or in another data frame using python

查看:81
本文介绍了使用python搜索数据框的另一列或另一个数据框中是否存在任何单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个如下所示的数据框

Hi I have two DataFrames like below

 DF1

 Alpha   |  Numeric  |  Special

 and     |  1        |   @
 or      |  2        |   $
         |  3        |   &  
         |  4        |     
         |  5        |     

DF2 with single column

Content      |

boy or girl  |
school @ morn|

我想搜索DF1列中是否有人在DF2内容列中有关键字并且输出应该在新的DF中

I want to search if anyone of the column in DF1 has anyone of the keyword in content column of DF2 and the output should be in a new DF

 output_DF

 output_column|
 Alpha        |
 Special      |

有人帮我解决这个问题

推荐答案

您可以对df1中的每一列应用 Series.isin()方法,然后返回出现任何情况的列名:

You could apply the Series.isin() method for each column in df1 and then return the column names for which there are any occurrences:

import pandas as pd

d = {'Alpha' :['and', 'or'],'Numeric':[1, 2,3,4,5],'Special':['@', '$','&']}
df1 = pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in d.iteritems() ]))

df2 = pd.DataFrame({'Content' :['boy or girl','school @ morn']})    

check = lambda r:[c for c in df1.columns if df1[c].dropna().isin(r).any()]
df3 = pd.DataFrame({'output_column' : df2["Content"].str.split(' ').apply(check)})

这将导致:

  output_column
0       [Alpha]
1     [Special]

这篇关于使用python搜索数据框的另一列或另一个数据框中是否存在任何单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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