0

so I am trying to make a boolean search program. The user enters a keyword (at most 3) to search in my dataframe. So, to search the keyword in my columns, I am using .loc and this does return the keyword but it also returns similar words too. For example:

The user enters the keyword - tweet

the result shows - tweet and tweeted

Dataframe has 4 columns (One_key, Two_key, Three_key, link). 10 keywords in each _key column. I am taking data from some text files but this is the format.

,One_key,Two_key,Third_key,link
0,parliament,laws,agricultural,www.xyz.com
1,population,supreme court,ampc,www.zyx.com
2,protest,road,blockades,www.fdw.com
3,violence,tweeted,batons,www.jsa.com
4,Rihanna,tweet,government, www.xyz.com
5,barbadian,Chris Brown,together, www.ici.com


if x == '1':
    print("enter the word to search:")
    y = input()
    print("Your search result: \n", keyword.loc[(keyword.One_key.str.contains(y, )) 
                                                | (keyword.Two_key.str.contains(y)) 
                                                | (keyword.Third_key.str.contains(y))])

What should I use to get the exact word?

deeCube
  • 1
  • 1
  • If you could provide a small sample data, that would be great. But you need to use `df[col].str.extract(r"pattern")` and inside the pattern you need to do grouping. This is just a general approach. If you put a small dataframe in your question, we can help you better. – ashkangh Mar 07 '21 at 22:00
  • Ah sorry, I am new and I couldn't find where or how to upload data. I will try to upload the data asap but thanks for the help! :D – deeCube Mar 07 '21 at 23:19
  • It's fine! [This](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) might help you. – ashkangh Mar 07 '21 at 23:23

1 Answers1

0

Here, this could work :

keyword[['One_key', 'Two_key', 'Three_key']].sum(axis=1).str.contains(y)
Icarwiz
  • 184
  • 6
  • It didn't work, still showed two results but I tried solving it with the logical operators and it worked but I want to search for two keywords with AND and I am not sure how to do it. I am taking two variables y and z. Would it be like this? keyword[(keyword["One_key"]==y) | (keyword["Two_key"]== y) |(keyword["Three_key"]==y)] & keyword[(keyword["One_key"]==z) | (keyword["Two_key"]== z) |(keyword["Three_key"]==z)] – deeCube Mar 07 '21 at 23:17