numpy int64不可迭代

时间:2019-06-10 16:12:11

标签: python numpy

好的,所以我有这个问题: 我有一个python脚本,该脚本使用pytrends随时间显示来自Google兴趣的一些数据。该函数不输出列表,而是输出一堆numpy.int64数字。我对他们无能为力,你们能帮我吗?

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”输出一堆数字。我要他们的钱。

1 个答案:

答案 0 :(得分:2)

您每次循环都在计算总和。从循环中删除总和。

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

同样要注意的是,一旦到达第一个附加start已经设置为2-我认为这是不可取的。

数据也已经存在于DataFrame对象中,为什么还要麻烦将其添加到列表中?

sumFirstHalf = IOT['x'].iloc[:12].sum()