Skip to content

Commit 6f1b5ad

Browse files
authored
Merge pull request GeneralMills#276 from GeneralMills/revert-275-revert-264-master
Revert "Revert "Added ability to return Rising Topics""
2 parents 6aa46ea + 90d8d09 commit 6f1b5ad

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

pytrends/request.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import sys
55

6+
from pandas.io.json.normalize import nested_to_record
67
import pandas as pd
78
import requests
89

@@ -270,13 +271,21 @@ def related_topics(self):
270271

271272
# top topics
272273
try:
273-
df = pd.DataFrame(req_json['default']['rankedList'][0]['rankedKeyword'])
274-
df = pd.DataFrame(df['topic'].tolist()).join(df[['value']])
274+
top_list = req_json['default']['rankedList'][0]['rankedKeyword']
275+
df_top = pd.DataFrame([nested_to_record(d, sep='_') for d in top_list])
275276
except KeyError:
276277
# in case no top topics are found, the lines above will throw a KeyError
277-
df = None
278+
df_top = None
278279

279-
result_dict[kw] = df
280+
#rising topics
281+
try:
282+
rising_list = req_json['default']['rankedList'][1]['rankedKeyword']
283+
df_rising = pd.DataFrame([nested_to_record(d, sep='_') for d in rising_list])
284+
except KeyError:
285+
# in case no rising topics are found, the lines above will throw a KeyError
286+
df_rising = None
287+
288+
result_dict[kw] = {'rising': df_rising, 'top' : df_top}
280289
return result_dict
281290

282291
def related_queries(self):

0 commit comments

Comments
 (0)