Skip to content

Instantly share code, notes, and snippets.

@franc3000
Created January 9, 2018 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franc3000/23d2b10e8685e2a43c787803dbdd3c5f to your computer and use it in GitHub Desktop.
Save franc3000/23d2b10e8685e2a43c787803dbdd3c5f to your computer and use it in GitHub Desktop.
# empty dictionary for storing DataFrames
results = {}
# list of params for query
my_list = ['MX19', 'MX20', 'MX21', 'MX22', 'MX23', 'MX24', 'MX25', 'MX26', 'MX27', 'MX28', 'MX29', 'MX30', 'MX31', 'MX32']
for x in my_list:
ADM = """
SELECT a.SQLDATE,
sum(if(a.QuadClass=2 AND a.AvgTone>0,1, 0)) as coop,
sum(if(a.QuadClass=4 AND a.AvgTone<0,1, 0)) as conf,
sum(if(a.EventRootCode='14' AND a.Quadclass!=2 AND a.Quadclass!=4,1,0)) as protest,
FROM [gdelt-bq:gdeltv2.events] AS a
WHERE a.IsRootEvent=1
AND (a.SQLDATE >20160101 AND a.SQLDATE<20171220)
AND a.ActionGeo_ADM1Code='{param}'
GROUP BY 1
ORDER BY 1
""".format(param=x)
# store gbq results in dictionary with param as the lookup
results[x] = gbq.read_gbq(ADM, project_id="flowing-sign-127521")
# iterate through the list of params
for param in my_list:
print(results[param])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment