如何使用python中的pandas将sevaral关键字与数据帧列值映射

时间:2017-10-03 06:33:09

标签: python pandas dataframe contains data-analysis

您好我有一个关键字列表。

keyword_list=['one','two']

DF,

    Name      Description
    Sri       Sri is one of the good singer in this two
    Ram       Ram is one of the good cricket player

我想找到包含来自keyword_list的所有值的行。

我想要的输出是,

output_Df,
    Name    Description
    Sri     Sri is one of the good singer in this two

I tried, mask=DF['Description'].str.contains() method but I can do this only for a single word pls help.

1 个答案:

答案 0 :(得分:2)

使用list comprehension创建的所有掩码的np.logical_and + reduce

keyword_list=['one','two']

m = np.logical_and.reduce([df['Description'].str.contains(x) for x in keyword_list])
df1 = df[m]
print (df1)

  Name                                Description
0  Sri  Sri is one of the good singer in this two

面具的替代品:

m = np.all([df['Description'].str.contains(x) for x in keyword_list], axis=0)

#if no NaNs
m = [set(x.split()) >= set(keyword_list) for x in df['Description']]