Python TypeError: dict上的字符串索引必须是整数



我有一个问题。我有一个字典,我想把它变成一个数据框,我不确定我做错了什么。

over_time_req_payload = {
'req': json.dumps(interest_over_time_widget['request']),
'token': interest_over_time_widget['token'],
'tz': timezone
}
req_json_interest_over_time = requests.get(interest_over_time_url,
params=over_time_req_payload,
cookies=cookies,
headers={'accept-language': 'us'})
iot_trimmed = req_json_interest_over_time.text[len(data_trim):]
df = pd.DataFrame(iot_trimmed['default']['timelineData'])
df['date'] = pd.to_datetime(df['time'].astype(dtype='float64'), unit='s')
df = df.set_index(['date']).sort_index()
result_df = df['value'].apply(lambda x: pd.Series( str(x).replace('[', '').replace(']', '').split(','))

我得到这个错误:

Exception has occurred: TypeError       (note: full exception trace is shown but execution is paused at: <module>)
string indices must be integers
File "C:UsersraykoDevProjectsgoogleExamplesauxilary_filesgTrendsUtil_simple_working.py", line 74, in <module> (Current frame)
df = pd.DataFrame(iot_trimmed['default']['timelineData'])

pd不应该。Dataframe取来自json请求的字典,然后在我设置索引和类型之后,使其可用,这样我就可以打印。head()

我不知道我错过了什么,但我在这方面很新,所以这可能是显而易见的。所有我在网上找到的事实是,我需要索引字符串,但json返回一个字典。下面是我打印iot_trim时的一小段摘录。

{"default":{"timelineData":[{"time":"1604188800","formattedTime":"Nov 1 u2013 7, 2020","formattedAxisTime":"Nov 1, 2020","value":[19],"hasData":[true],"formattedValue":["19"]},{"time":"1604793600","formattedTime":"Nov 8 u2013 14, 2020","formattedAxisTime":"Nov 8, 2020","value":[21],"hasData":[true],"formattedValue":["21"]},{"time":"1605398400","formattedTime":"Nov 15 u2013 21, 2020","formattedAxisTime":"Nov 15, 2020","value":[32],"hasData":[true],"formattedValue":["32"]},{"time":"1606003200","formattedTime":"Nov 22 u2013 28, 2020","formattedAxisTime":"Nov 22, 2020","value":[36],"hasData":[true],"formattedValue":["36"]},{"time":"1606608000","formattedTime":"Nov 29 u2013 Dec 5, 2020","formattedAxisTime":"Nov 29, 2020","value":[32],"hasData":[true],"formattedValue":["32"]},{"time":"1607212800","formattedTime":"Dec 6 u2013 12, 2020","formattedAxisTime":"Dec 6, 2020","value":[24],"hasData":[true],"formattedValue":["24"]},{"time":"1607817600","formattedTime":"Dec 13 u2013 19, 2020","formattedAxisTime":"Dec 13, 2020","value":[42],"hasData":[true],"formattedValue":["42"]},{"time":"1608422400","formattedTime":"Dec 20 u2013 26, 2020","formattedAxisTime":"Dec 20, 2020","value":[41],"hasData":[true],"formattedValue":["41"]},{"time":"1609027200","formattedTime":"Dec 27, 2020 u2013 Jan 2, 2021","formattedAxisTime":"Dec 27, 2020","value":[61],"hasData":[true],"formattedValue":["61"]},{"time":"1609632000","formattedTime":"Jan 3 u2013 9, 2021","formattedAxisTime":"Jan 3, 2021","value":[100],"hasData":[true],"formattedValue":["100"]},{"time":"1610236800","formattedTime":"Jan 10 u2013 16, 2021","formattedAxisTime":"Jan 10, 2021","value":[97],"hasData":[true],"formattedValue":["97"]},{"time":"1610841600","formattedTime":"Jan 17 u2013 23, 2021","formattedAxisTime":"Jan 17, 2021","value":[61],"hasData":[true],"formattedValue":["61"]},{"time":"1611446400","formattedTime":"Jan 24 u2013 30, 2021","formattedAxisTime":"Jan 24, 2021","value":[60],"hasData":[true],"formattedValue":["60"]},{"time":"1612051200","formattedTime":"Jan 31 u2013 Feb 6, 2021","formattedAxisTime":"Jan 31, 2021","value":[53],"hasData":[true],"formattedValue":["53"]},{"time":"1612656000","fo

iot_trimmed = req_json_interest_over_time.text[len(data_trim):]正在给您一个字符串(requests.get()响应的文本部分,第一部分被修剪掉。)

Python需要对象为dict,以便从中提取给定的键。看起来请求响应对象有一个.json()方法,它给你一个字典。

所以试着用iot_trimmed = req_json_interest_over_time.json()

相关内容

  • 没有找到相关文章

最新更新