Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support geo codes like 'US-<state>-<geo-code>' #386

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions pytrends/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,22 @@ def interest_by_region(self, resolution='COUNTRY', inc_low_vol=False,
df = pd.DataFrame(req_json['default']['geoMapData'])
if (df.empty):
return df

# rename the column with the search keyword
df = df[['geoName', 'geoCode', 'value']].set_index(
['geoName']).sort_index()
# split list columns into separate ones, remove brackets and split on comma
# only if the json has 'geoCode'
has_geo = False
if 'geoCode' in df.columns:
df = df[['geoName', 'geoCode', 'value']].set_index(['geoName']).sort_index()
has_geo = True
# if not, do not use it
else:
df = df[['geoName', 'value']].set_index(['geoName']).sort_index()
# split list columns into seperate ones, remove brackets and split on comma

result_df = df['value'].apply(lambda x: pd.Series(
str(x).replace('[', '').replace(']', '').split(',')))
if inc_geo_code:
# if 'geoCode' exists, add it here
if inc_geo_code and has_geo:
result_df['geoCode'] = df['geoCode']

# rename each column with its search term
Expand Down