0

Ok so I have this problem: I have a python script that uses pytrends to show some data from Google interest over time. The function outputs not a list, but a bunch of numpy.int64 numbers. I can't do anything with them, can you guys help me out?

from pytrends.request import TrendReq
import datetime
pytrend = TrendReq(hl='en-US', tz=360)
import numpy

kw_list=['x']
pytrend.build_payload(kw_list)

now = datetime.datetime.now()
nowyear = now.year
nowmonth = now.month
nowday = now.day
endday = now.day - 1

start = 0

IOT = pytrend.get_historical_interest(kw_list, year_start=nowyear, month_start=nowmonth, 
    day_start=endday, hour_start=0, 
    year_end=nowyear, month_end=nowmonth, 
    day_end=nowday, hour_end=0, 
    cat=0, geo='', gprop='', sleep=0)

firstHalf = []
secondHalf = []

while start < 24:
    start += 1
    while start < 12:
        start += 1
        firstHalf.append(IOT['x'].iloc[start])
        firstHalfSum = (firstHalf[len(firstHalf) - 1])
        print(firstHalfSum)

"firstHalfSum" outputs a bunch of numbers. I want their sum.

1 Answer 1

2

You're calculating the sum every time you loop. Remove the sum from the loop.

while start < 24:
    start += 1
    while start < 12:
        start += 1
        firstHalf.append(IOT['x'].iloc[start])
    print(sum(firstHalf))

Also a heads up that once you reach the first append start is already set to 2 - I assume this is undesirable.

Also the data is already in a DataFrame object why bother adding it to a list?

sumFirstHalf = IOT['x'].iloc[:12].sum()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.