hulyacobans / google-trends-to-sheets

19 stars 14 forks source link

TypeError: cannot concatenate object of type '<class 'dict'>'; only Series and DataFrame objs are valid #1

Open OrsoArmeno opened 4 years ago

OrsoArmeno commented 4 years ago

Hello,

I am following religiously your tutorial, that is GREAT.

I ended up in having an issue though.

When I try to concatenate the results of my multiple searches result = pd.concat(dicti, axis=1) result.columns = result.columns.droplevel(0) result = result.drop('isPartial', axis = 1)

I receive this error TypeError: cannot concatenate object of type '<class 'dict'>'; only Series and DataFrame objs are valid

I have understood that Python doesn't allow me to concatenate the results of my searches, but I have no idea how to overcome this issue. Can you maybe help me?

(this is my first request on GitHub, so I apologize in advance if lacks the "etiquette" of this place)

hichnicksemen commented 4 years ago

Hi Hulya, i have the same question. Can you explain why we can't concatenate dict here?

OrsoArmeno commented 4 years ago

hi @hichnicksemen . i have found a workaround since when I wrote my issue:

import pytrends
from pytrends.request import TrendReq
import pandas as pd
import time
import datetime
from datetime import datetime, date, time

pytrend = TrendReq()

searches = ['kw1', 'kw2', 'kw2', 'kw3']

groupkeywords = list(zip(*[iter(searches)]*1))
groupkeywords = [list(x) for x in groupkeywords]

dicti = {}
i = 1
for trending in groupkeywords:
    pytrend.build_payload(trending, geo = 'US', timeframe = 'today 3-m', cat = 1)
    dicti[i] = pytrend.interest_over_time()
    i+=1

result = pd.concat(dicti, axis=1)
result.columns = result.columns.droplevel(0)
result = result.drop('isPartial', axis = 1)
result.reset_index(level=0, inplace=True)
pd.melt(result, id_vars='date', value_vars=searches)
print(result)

I hope it works

hichnicksemen commented 4 years ago

@OrsoArmeno Thanks, i'll try!