I have a Dataframe being returned from a google trends API and contains values for date, keyword and search volume. I need to return a list of lists that will contain the following keyword, date 1, value 1, date 2, value 2, date 3, value 3, date n, value n...]
I have the following function that will take a set of keywords and send them to the API, then converts the returned dataframe to a list
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)
The following screenshot1 shows what the output looks like as a dataframe
That gives the list output [[[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]
I have transposed the dataframe by updating these two lines
df = (pytrends.interest_over_time())
google_data_list = df_.values.tolist()
to
df_new = df.transpose()
google_data_list = df_new.values.tolist()
Screenshot2 shows what this table looks like
and it
which creates the list output for the first two values:
[[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]
So for the first example my desired output would be
[0 balance transfer, date1, 1, date2, 1, date3, 1, dateN, 1...]
But I am struggling to take the date from the header and adding it alongside the corresponding value for the list. Any help much appreciated.
