pytrends combinatorial search

79 Views Asked by At

I am working on Google Trends. Since I am new to the package I am trying to play around with it. For instance I here formed my first trend search:

from pytrends.request import TrendReq

pytrends = TrendReq(hl='en-US', tz=360) 

kw_list = ["Machine Learning", "scarf", "AI", "amazon"] # list of keywords to get data 

pytrends.build_payload(kw_list, cat=0, timeframe='today 12-m') 

#1 Interest over Time
data = pytrends.interest_over_time() 
data = data.reset_index() 


import plotly.express as px

fig = px.line(data, x="date", y=['amazon'], title='Keyword Web Search Interest Over Time')
fig.show() 

What I would like to do, however is to perform combinatorial searches. So basically, in the example above rather than searching for the trends of single words each, I would like to perform the search of all the combination of such words:

  1. 4 elements: "Machine Learning scarf AI amazon"
  2. 3 elements: "Machine Learning scarf AI"; Machine Learning scarf amazon", "AI scarf amazon"
  3. 2 elements: Machine Learning scarf", "Machine Learning AI"; "Machine Learning amazon"; "amazon AI"; "amazon scarf"; "scarf AI";...

I think I need itertools here to get all possible combinations but while I looked for it for combinations of strings letter I am not sure on how to use it for obtaining the desired combinations and input them in GT.

I guess something like this:

temp1 = itertools.combinations(kw_list, 2)
temp1 = list(temp1)

may work for obtaining only the combinations of 2 words. However it remains quite unclear how to use them in GT and wrap them up in a pandas dataframe.

Can someone please help me out on this?

0

There are 0 best solutions below