我有一个从Google趋势API返回的数据框,其中包含日期,关键字和搜索量的值。我需要返回一个列表列表,其中将包含以下keyword, date 1, value 1, date 2, value 2, date 3, value 3, date n, value n...]
我具有以下功能,该功能将使用一组关键字并将其发送到API,然后将返回的数据帧转换为列表
def list_to_api(keyword_list):
(pytrends.build_payload(keyword_list, cat=0, timeframe='today 12-m', geo='', gprop=''))
df = (pytrends.interest_over_time())
google_data_list = df.values.tolist()
print(type(google_data_list))
print("Resting 5 seconds for next API Call")
print("Converted to list ")
insert_list.append(google_data_list)
以下屏幕截图1显示了输出作为数据框的样子
给出列表输出[[[1, 93, 29, 7, 0, False], [1, 95, 31, 8, 0, False], [1, 91, 31, 8, 0, False], [1, 93, 34, 7, 0, False], [1, 96, 32, 8, 0, False]
我通过更新这两行来换置数据框
df = (pytrends.interest_over_time())
google_data_list = df_.values.tolist()
到
df_new = df.transpose()
google_data_list = df_new.values.tolist()
截图2显示了该表格的外观
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[92, 94, 92, 94, 98, 100, 85, 87, 88, 87, 95, 89, 89, 93, 94, 88, 86, 87, 84,
87, 82, 80, 81, 81, 76, 78, 78, 77, 73, 77, 76, 76, 79, 73, 87, 88, 91, 92, 88, 90,
85, 88, 95, 94, 89, 91, 91, 91, 89, 85, 86]
所以对于第一个示例,我想要的输出将是
[0 balance transfer, date1, 1, date2, 1, date3, 1, dateN, 1...]
但是我正在努力从标题中获取日期,并将其添加到列表的相应值旁边。任何帮助表示赞赏。
答案 0 :(得分:1)
例如,您可以使用循环和列表理解来代替transpose()
和tolist()
df = pd.DataFrame([[1, 93, 29, 7, 0, False], [1, 95, 31, 8, 0, False], [1, 91, 31, 8, 0, False], [1, 93, 34, 7, 0, False], [1, 96, 32, 8, 0, False]])
df.columns = ['0 balance transfer', 'car insurance', 'travel insurance', 'pet insurance', 'ww travel insurance', 'isPartial']
df.index = ['2018-05-06','2018-05-13','2018-05-20','2018-05-27','2018-06-03']
out =[]
for col in df:
tmp = [col]
[tmp.extend((date, value)) for date, value in zip(df[col].index, df[col])]
out.append(tmp)
print(out)
>> [['0 balance transfer', '2018-05-06', 1, '2018-05-13', 1, '2018-05-20', 1, '2018-05-27', 1, '2018-06-03', 1], ['car insurance', '2018-05-06', 93, '2018-05-13', 95, '2018-05-20', 91, '2018-05-27', 93, '2018-06-03', 96], ['travel insurance', '2018-05-06', 29, '2018-05-13', 31, '2018-05-20', 31, '2018-05-27', 34, '2018-06-03', 32], ['pet insurance', '2018-05-06', 7, '2018-05-13', 8, '2018-05-20', 8, '2018-05-27', 7, '2018-06-03', 8], ['ww travel insurance', '2018-05-06', 0, '2018-05-13', 0, '2018-05-20', 0, '2018-05-27', 0, '2018-06-03', 0], ['isPartial', '2018-05-06', False, '2018-05-13', False, '2018-05-20', False, '2018-05-27', False, '2018-06-03', False]]
根据评论编辑(删除isPartial列和过滤日期):
del df['isPartial']
out =[]
for col in df:
tmp = [col]
[tmp.extend((date, value)) for date, value in zip(df[col].index, df[col]) if date > '2018-05-15']
out.append(tmp)
print(out)