Big Query requires a special package bigquery that allows us to connect to the database via an API using our credentials. We will be able to see things about any database we select and run queries against it using SQL to extract information. This is the most popular way of working with BigQuery datasets in Python, where we can subsequently able to do further analysis on it.

This page shows how to get started with the Cloud Client Libraries for the BigQuery API. Read more about the client libraries for Cloud APIs, including the older Google APIs Client Libraries, in Client Libraries Explained.

Step 1: Installing the BigQuery API Client Library

First, we need to create a Google BigQuery account, then we need to install the library via Anaconda Prompt.

pip install --upgrade google-cloud-bigquery

Next, we must create a service account. Once we have done that and downloaded our API key we need to point Windows PowerShell to our authorisation credentials.$env:GOOGLE_APPLICATION_CREDENTIALS=”C:\Users\Ags91\Jupyter Notebooks\Data Analyst Portfolio\API Keys\plenary-edition-309221-3adba393ed83.json”

The same command can also be executed in the Command Prompt as:set GOOGLE_APPLICATION_CREDENTIALS=”C:\Users\Ags91\Jupyter Notebooks\Data Analyst Portfolio\API Keys\plenary-edition-309221-3adba393ed83.json”

We are also going to install a library called Big Query Helper, this helps us simplify common read-only tasks.

pip install -e git+https://github.com/SohierDane/BigQuery_Helper#egg=bq_helper

Step 2: Importing Libraries

These are the libraries we require to work with SQL in python.

# Import libraries
import pandas as pd
import numpy as np 
import matplotlib.pyplot as plt
import seaborn as sns
import os
import bq_helper as BigQueryHelper
from google.cloud import bigquery

# This will set the limit of rows to 500
pd.options.display.max_rows = 500
# Setting the backup variable for API key in case the PowerShell does not work
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=r"C:\Users\Ags91\Jupyter Notebooks\Data Analyst Portfolio\API Keys\plenary-edition-309221-3adba393ed83.json"

# Create a "Client" object
client = bigquery.Client()

# load magic command that lets us run queries with minimal code
%load_ext google.cloud.bigquery

Step 3: Importing from SQL Database

The data we are going to query using SQL is hosted in Google BigQuery. London is the capital and most populous city of England and the United Kingdom. Standing on the River Thames in the southeast of the island of Great Britain, London has been a major settlement for two millennia. Source: https://en.wikipedia.org/wiki/London.

This data counts the number of crimes at two different geographic levels of London (LSOA and borough) by year, according to crime type. Includes data from 2008 to the present.

Below is a list of the crime types covered (*not available at LSOA level):

Major Category: Minor Category

  • Burglary: Burglary in a Dwelling (data to March 2017)
  • Burglary: Burglary in Other Buildings (data to March 2017)
  • Burglary: Burglary – Residential (as of April 2017 Burglary in a Dwelling was changed to this subcategory to take account of certain offences being reclassified from what was the ‘Other buildings’ category (now Business and Community) into this new category eg. burglaries of shed and outbuildings). The old and new subcategories are therefore not comparable.
  • Burglary: Burglary – Business and Community (as of April 2017 Burglary in Other Buildings was changed to this subcategory to take account of certain offences being reclassified to what is now the ‘Burglary Residential’ category. The old and new subcategories are therefore not comparable.
  • Criminal Damage: Criminal Damage To Dwelling
  • Criminal Damage: Criminal Damage To Motor Vehicle
  • Criminal Damage: Criminal Damage To Other Building
  • Criminal Damage: Other Criminal Damage
  • Drugs: Drug Trafficking
  • Drugs: Other Drugs
  • Drugs: Possession Of Drugs
  • Fraud or Forgery: Counted per Victim*
  • Fraud or Forgery: Other Fraud & Forgery
  • Other Notifiable Offences: Going Equipped
  • Other Notifiable Offences: Other Notifiable
  • Robbery: Business Property
  • Robbery: Personal Property
  • Sexual Offences: Other Sexual
  • Sexual Offences: Rape
  • Theft and Handling: Handling Stolen Goods
  • Theft and Handling: Motor Vehicle Interference & Tampering
  • Theft and Handling: Other Theft
  • Theft and Handling: Other Theft Person
  • Theft and Handling: Theft From Motor Vehicle
  • Theft and Handling: Theft From Shops
  • Theft and Handling: Theft/Taking of Pedal Cycle
  • Violence Against the Person: Assault with Injury
  • Violence Against the Person: Common Assault
  • Violence Against the Person: Grievous Bodily Harm
  • Violence Against the Person: Harassment
  • Violence Against the Person: Murder
  • Violence Against the Person: Offensive Weapon
  • Violence Against the Person: Other violence
  • Violence Against the Person: Wounding/GBH

Column descriptions

lsoa_code: Lower Layer Super Output Area code according to the Office for National Statistics
borough: A town or district which is an administrative unit
major_category: Major crime category
minor_category: Minor crime category
value: Summary of the number of crimes for the month
year: Year of the record
month: The month of the record

%%bigquery
SELECT *
FROM bigquery-public-data.london_crime.crime_by_lsoa
LIMIT 10
Query complete after 0.00s: 100%|███████████████████████████████████████████████████| 1/1 [00:00<00:00, 1008.97query/s]
Downloading: 100%|███████████████████████████████████████████████████████████████████| 10/10 [00:00<00:00, 10.91rows/s]
lsoa_codeboroughmajor_categoryminor_categoryvalueyearmonth
0E01032740City of LondonViolence Against the PersonHarassment0201611
1E01000005City of LondonViolence Against the PersonHarassment020113
2E01032739City of LondonViolence Against the PersonHarassment0201612
3E01000005City of LondonViolence Against the PersonHarassment0201111
4E01000005City of LondonViolence Against the PersonHarassment020143
5E01000002City of LondonViolence Against the PersonHarassment020115
6E01000005City of LondonViolence Against the PersonHarassment020118
7E01000005City of LondonViolence Against the PersonHarassment020133
8E01032740City of LondonViolence Against the PersonHarassment0200911
9E01032739City of LondonViolence Against the PersonHarassment020081

Step 4: Questions

Question 1: What is the change in the number of crime incidents from 2011 to 2016?

%%bigquery
SELECT SUM(value) AS total_crimes, year 
FROM bigquery-public-data.london_crime.crime_by_lsoa
WHERE year between 2011 and 2016
GROUP BY year
ORDER BY year

Query complete after 0.00s: 100%|███████████████████████████████████████████████████| 1/1 [00:00<00:00, 2014.56query/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████| 6/6 [00:00<00:00,  6.50rows/s]
total_crimesyear
07249152011
17373292012
26864072013
36801832014
47116242015
57361212016

Question 2: What were the top 3 crimes per borough in 2016?

To answer this question, first, we need to find all the boroughs included in our data.

%%bigquery
SELECT DISTINCT borough
FROM bigquery-public-data.london_crime.crime_by_lsoa
Query complete after 0.00s: 100%|███████████████████████████████████████████████████| 2/2 [00:00<00:00, 1344.54query/s]
Downloading: 100%|███████████████████████████████████████████████████████████████████| 33/33 [00:01<00:00, 32.94rows/s]
borough
0City of London
1Brent
2Barnet
3Bexley
4Camden
5Ealing
6Harrow
7Merton
8Newham
9Sutton
10Bromley
11Croydon
12Enfield
13Hackney
14Lambeth
15Haringey
16Havering
17Hounslow
18Lewisham
19Greenwich
20Islington
21Redbridge
22Southwark
23Hillingdon
24Wandsworth
25Westminster
26Tower Hamlets
27Waltham Forest
28Barking and Dagenham
29Kingston upon Thames
30Richmond upon Thames
31Hammersmith and Fulham
32Kensington and Chelsea
top_crimes_borough_query = """
SELECT borough, major_category, SUM(value) AS total_crimes
FROM bigquery-public-data.london_crime.crime_by_lsoa
WHERE year = 2016
GROUP BY borough, major_category
ORDER BY borough, total_crimes DESC;
        """
%%bigquery
SELECT borough, major_category, SUM(value) AS total_crimes
FROM bigquery-public-data.london_crime.crime_by_lsoa
WHERE year = 2016
GROUP BY borough, major_category
ORDER BY borough, total_crimes DESC;
Query complete after 0.00s: 100%|███████████████████████████████████████████████████| 3/3 [00:00<00:00, 2005.24query/s]
Downloading: 100%|████████████████████████████████████████████████████████████████| 295/295 [00:00<00:00, 345.68rows/s]
boroughmajor_categorytotal_crimes
0Barking and DagenhamViolence Against the Person6067
1Barking and DagenhamTheft and Handling5607
2Barking and DagenhamCriminal Damage1949
3Barking and DagenhamBurglary1287
4Barking and DagenhamDrugs919
5Barking and DagenhamRobbery534
6Barking and DagenhamOther Notifiable Offences378
7Barking and DagenhamSexual Offences0
8Barking and DagenhamFraud or Forgery0
9BarnetTheft and Handling9731
10BarnetViolence Against the Person7499
11BarnetBurglary3402
12BarnetCriminal Damage2183
13BarnetDrugs906
14BarnetOther Notifiable Offences499
15BarnetRobbery464
16BarnetSexual Offences0
17BarnetFraud or Forgery0
18BexleyViolence Against the Person4503
19BexleyTheft and Handling4392
20BexleyCriminal Damage1673
21BexleyBurglary1123
22BexleyDrugs646
23BexleyOther Notifiable Offences294
24BexleyRobbery209
25BexleySexual Offences0
26BexleyFraud or Forgery0
27BrentViolence Against the Person9205
28BrentTheft and Handling9026
29BrentBurglary2631
30BrentCriminal Damage2280
31BrentDrugs2096
32BrentRobbery919
33BrentOther Notifiable Offences536
34BrentSexual Offences0
35BrentFraud or Forgery0
36BromleyTheft and Handling7584
37BromleyViolence Against the Person6650
38BromleyBurglary2214
39BromleyCriminal Damage2202
40BromleyDrugs728
41BromleyOther Notifiable Offences417
42BromleyRobbery369
43BromleySexual Offences0
44BromleyFraud or Forgery0
45CamdenTheft and Handling14088
46CamdenViolence Against the Person7626
47CamdenBurglary2652
48CamdenCriminal Damage1935
49CamdenDrugs1493
50CamdenRobbery899
51CamdenOther Notifiable Offences490
52CamdenSexual Offences0
53CamdenFraud or Forgery0
54City of LondonTheft and Handling129
55City of LondonViolence Against the Person25
56City of LondonDrugs10
57City of LondonOther Notifiable Offences6
58City of LondonRobbery4
59City of LondonCriminal Damage2
60City of LondonBurglary2
61CroydonViolence Against the Person10302
62CroydonTheft and Handling9229
63CroydonCriminal Damage3219
64CroydonBurglary2738
65CroydonDrugs1367
66CroydonRobbery1139
67CroydonOther Notifiable Offences718
68CroydonSexual Offences0
69CroydonFraud or Forgery0
70EalingTheft and Handling10040
71EalingViolence Against the Person9396
72EalingCriminal Damage2562
73EalingBurglary2492
74EalingDrugs1355
75EalingRobbery669
76EalingOther Notifiable Offences613
77EalingSexual Offences0
78EalingFraud or Forgery0
79EnfieldTheft and Handling8037
80EnfieldViolence Against the Person7409
81EnfieldBurglary2541
82EnfieldCriminal Damage2136
83EnfieldDrugs1063
84EnfieldRobbery807
85EnfieldOther Notifiable Offences492
86EnfieldSexual Offences0
87EnfieldFraud or Forgery0
88GreenwichViolence Against the Person8590
89GreenwichTheft and Handling8010
90GreenwichCriminal Damage2476
91GreenwichBurglary1780
92GreenwichDrugs867
93GreenwichOther Notifiable Offences521
94GreenwichRobbery486
95GreenwichSexual Offences0
96GreenwichFraud or Forgery0
97HackneyTheft and Handling11851
98HackneyViolence Against the Person8832
99HackneyBurglary2719
100HackneyCriminal Damage1981
101HackneyDrugs1353
102HackneyRobbery1030
103HackneyOther Notifiable Offences499
104HackneySexual Offences0
105HackneyFraud or Forgery0
106Hammersmith and FulhamTheft and Handling8925
107Hammersmith and FulhamViolence Against the Person6118
108Hammersmith and FulhamBurglary1531
109Hammersmith and FulhamCriminal Damage1408
110Hammersmith and FulhamDrugs1321
111Hammersmith and FulhamOther Notifiable Offences474
112Hammersmith and FulhamRobbery397
113Hammersmith and FulhamSexual Offences0
114Hammersmith and FulhamFraud or Forgery0
115HaringeyTheft and Handling10554
116HaringeyViolence Against the Person8574
117HaringeyBurglary2474
118HaringeyCriminal Damage2233
119HaringeyDrugs1540
120HaringeyRobbery1248
121HaringeyOther Notifiable Offences551
122HaringeySexual Offences0
123HaringeyFraud or Forgery0
124HarrowTheft and Handling4537
125HarrowViolence Against the Person4293
126HarrowBurglary1994
127HarrowCriminal Damage1212
128HarrowDrugs473
129HarrowRobbery377
130HarrowOther Notifiable Offences267
131HarrowSexual Offences0
132HarrowFraud or Forgery0
133HaveringViolence Against the Person5936
134HaveringTheft and Handling5919
135HaveringBurglary1826
136HaveringCriminal Damage1804
137HaveringDrugs718
138HaveringOther Notifiable Offences389
139HaveringRobbery311
140HaveringSexual Offences0
141HaveringFraud or Forgery0
142HillingdonTheft and Handling9045
143HillingdonViolence Against the Person7830
144HillingdonCriminal Damage2470
145HillingdonBurglary2229
146HillingdonOther Notifiable Offences1305
147HillingdonDrugs1042
148HillingdonRobbery420
149HillingdonSexual Offences0
150HillingdonFraud or Forgery0
151HounslowTheft and Handling8682
152HounslowViolence Against the Person7405
153HounslowCriminal Damage2213
154HounslowBurglary1808
155HounslowDrugs1086
156HounslowOther Notifiable Offences665
157HounslowRobbery392
158HounslowSexual Offences0
159HounslowFraud or Forgery0
160IslingtonTheft and Handling12077
161IslingtonViolence Against the Person7840
162IslingtonBurglary2051
163IslingtonCriminal Damage1903
164IslingtonDrugs1777
165IslingtonRobbery936
166IslingtonOther Notifiable Offences508
167IslingtonSexual Offences0
168IslingtonFraud or Forgery0
169Kensington and ChelseaTheft and Handling9855
170Kensington and ChelseaViolence Against the Person4696
171Kensington and ChelseaDrugs1680
172Kensington and ChelseaBurglary1449
173Kensington and ChelseaCriminal Damage1051
174Kensington and ChelseaRobbery523
175Kensington and ChelseaOther Notifiable Offences326
176Kensington and ChelseaSexual Offences0
177Kensington and ChelseaFraud or Forgery0
178Kingston upon ThamesTheft and Handling3803
179Kingston upon ThamesViolence Against the Person3194
180Kingston upon ThamesCriminal Damage1054
181Kingston upon ThamesBurglary879
182Kingston upon ThamesDrugs743
183Kingston upon ThamesOther Notifiable Offences189
184Kingston upon ThamesRobbery121
185Kingston upon ThamesSexual Offences0
186Kingston upon ThamesFraud or Forgery0
187LambethTheft and Handling13155
188LambethViolence Against the Person10496
189LambethBurglary3087
190LambethCriminal Damage2764
191LambethDrugs2738
192LambethRobbery1196
193LambethOther Notifiable Offences635
194LambethSexual Offences0
195LambethFraud or Forgery0
196LewishamViolence Against the Person8809
197LewishamTheft and Handling7702
198LewishamCriminal Damage2351
199LewishamBurglary2071
200LewishamDrugs1617
201LewishamRobbery869
202LewishamOther Notifiable Offences485
203LewishamSexual Offences0
204LewishamFraud or Forgery0
205MertonTheft and Handling4894
206MertonViolence Against the Person4026
207MertonBurglary1419
208MertonCriminal Damage1418
209MertonDrugs466
210MertonRobbery283
211MertonOther Notifiable Offences249
212MertonSexual Offences0
213MertonFraud or Forgery0
214NewhamTheft and Handling11964
215NewhamViolence Against the Person9646
216NewhamCriminal Damage2496
217NewhamBurglary2115
218NewhamDrugs1684
219NewhamRobbery1472
220NewhamOther Notifiable Offences713
221NewhamSexual Offences0
222NewhamFraud or Forgery0
223RedbridgeTheft and Handling7447
224RedbridgeViolence Against the Person6411
225RedbridgeBurglary1997
226RedbridgeCriminal Damage1650
227RedbridgeDrugs1017
228RedbridgeRobbery599
229RedbridgeOther Notifiable Offences381
230RedbridgeSexual Offences0
231RedbridgeFraud or Forgery0
232Richmond upon ThamesTheft and Handling4769
233Richmond upon ThamesViolence Against the Person3155
234Richmond upon ThamesBurglary1359
235Richmond upon ThamesCriminal Damage1148
236Richmond upon ThamesDrugs320
237Richmond upon ThamesOther Notifiable Offences217
238Richmond upon ThamesRobbery106
239Richmond upon ThamesSexual Offences0
240Richmond upon ThamesFraud or Forgery0
241SouthwarkTheft and Handling12946
242SouthwarkViolence Against the Person9474
243SouthwarkBurglary2946
244SouthwarkCriminal Damage2621
245SouthwarkDrugs1838
246SouthwarkRobbery1317
247SouthwarkOther Notifiable Offences494
248SouthwarkSexual Offences0
249SouthwarkFraud or Forgery0
250SuttonViolence Against the Person3714
251SuttonTheft and Handling3516
252SuttonCriminal Damage1316
253SuttonBurglary1233
254SuttonDrugs461
255SuttonOther Notifiable Offences253
256SuttonRobbery165
257SuttonSexual Offences0
258SuttonFraud or Forgery0
259Tower HamletsTheft and Handling10953
260Tower HamletsViolence Against the Person9608
261Tower HamletsBurglary2794
262Tower HamletsCriminal Damage2357
263Tower HamletsDrugs1629
264Tower HamletsRobbery1234
265Tower HamletsOther Notifiable Offences678
266Tower HamletsSexual Offences0
267Tower HamletsFraud or Forgery0
268Waltham ForestViolence Against the Person7409
269Waltham ForestTheft and Handling7357
270Waltham ForestCriminal Damage1989
271Waltham ForestBurglary1873
272Waltham ForestDrugs1042
273Waltham ForestRobbery602
274Waltham ForestOther Notifiable Offences444
275Waltham ForestSexual Offences0
276Waltham ForestFraud or Forgery0
277WandsworthTheft and Handling10789
278WandsworthViolence Against the Person6809
279WandsworthBurglary2351
280WandsworthCriminal Damage1836
281WandsworthDrugs870
282WandsworthRobbery609
283WandsworthOther Notifiable Offences415
284WandsworthSexual Offences0
285WandsworthFraud or Forgery0
286WestminsterTheft and Handling27520
287WestminsterViolence Against the Person10834
288WestminsterBurglary3218
289WestminsterCriminal Damage2179
290WestminsterDrugs2049
291WestminsterRobbery1822
292WestminsterOther Notifiable Offences708
293WestminsterSexual Offences0
294WestminsterFraud or Forgery0
%%bigquery
SELECT
  borough,
  major_category,
  rank_per_borough,
  no_of_incidents
FROM (
  SELECT
    borough,
    major_category,
    RANK() OVER(PARTITION BY borough ORDER BY SUM(value) DESC) AS rank_per_borough,
    SUM(value) AS no_of_incidents
  FROM
    `bigquery-public-data.london_crime.crime_by_lsoa`
  GROUP BY
    borough,
    major_category )
WHERE
  rank_per_borough <= 3
ORDER BY
  borough,
  rank_per_borough
Query complete after 0.00s: 100%|███████████████████████████████████████████████████| 4/4 [00:00<00:00, 2689.52query/s]
Downloading: 100%|██████████████████████████████████████████████████████████████████| 99/99 [00:00<00:00, 116.45rows/s]
boroughmajor_categoryrank_per_boroughno_of_incidents
0Barking and DagenhamTheft and Handling150999
1Barking and DagenhamViolence Against the Person243091
2Barking and DagenhamCriminal Damage318888
3BarnetTheft and Handling187285
4BarnetViolence Against the Person246565
5BarnetBurglary336981
6BexleyTheft and Handling140071
7BexleyViolence Against the Person230037
8BexleyCriminal Damage317244
9BrentTheft and Handling172523
10BrentViolence Against the Person263178
11BrentBurglary328923
12BromleyTheft and Handling169742
13BromleyViolence Against the Person246759
14BromleyBurglary327135
15CamdenTheft and Handling1140596
16CamdenViolence Against the Person253012
17CamdenBurglary327939
18City of LondonTheft and Handling1561
19City of LondonViolence Against the Person2114
20City of LondonDrugs333
21CroydonTheft and Handling191437
22CroydonViolence Against the Person267791
23CroydonBurglary333376
24EalingTheft and Handling193834
25EalingViolence Against the Person268492
26EalingBurglary330831
27EnfieldTheft and Handling170371
28EnfieldViolence Against the Person245036
29EnfieldBurglary330213
30GreenwichTheft and Handling164923
31GreenwichViolence Against the Person252897
32GreenwichCriminal Damage322755
33HackneyTheft and Handling191118
34HackneyViolence Against the Person256584
35HackneyBurglary321450
36Hammersmith and FulhamTheft and Handling186381
37Hammersmith and FulhamViolence Against the Person243014
38Hammersmith and FulhamBurglary317010
39HaringeyTheft and Handling183979
40HaringeyViolence Against the Person250943
41HaringeyBurglary328213
42HarrowTheft and Handling140800
43HarrowViolence Against the Person230213
44HarrowBurglary319630
45HaveringTheft and Handling152609
46HaveringViolence Against the Person233968
47HaveringBurglary321302
48HillingdonTheft and Handling180028
49HillingdonViolence Against the Person255264
50HillingdonBurglary326056
51HounslowTheft and Handling170180
52HounslowViolence Against the Person251404
53HounslowCriminal Damage321407
54IslingtonTheft and Handling1107661
55IslingtonViolence Against the Person252975
56IslingtonBurglary322207
57Kensington and ChelseaTheft and Handling195963
58Kensington and ChelseaViolence Against the Person229570
59Kensington and ChelseaBurglary314980
60Kingston upon ThamesTheft and Handling138226
61Kingston upon ThamesViolence Against the Person221540
62Kingston upon ThamesCriminal Damage310610
63LambethTheft and Handling1114899
64LambethViolence Against the Person272726
65LambethBurglary330199
66LewishamTheft and Handling170382
67LewishamViolence Against the Person263652
68LewishamBurglary324871
69MertonTheft and Handling144128
70MertonViolence Against the Person228322
71MertonBurglary316485
72NewhamTheft and Handling1106146
73NewhamViolence Against the Person266221
74NewhamBurglary325356
75RedbridgeTheft and Handling171496
76RedbridgeViolence Against the Person241430
77RedbridgeBurglary326735
78Richmond upon ThamesTheft and Handling140858
79Richmond upon ThamesViolence Against the Person220314
80Richmond upon ThamesBurglary316097
81SouthwarkTheft and Handling1109432
82SouthwarkViolence Against the Person268356
83SouthwarkBurglary327980
84SuttonTheft and Handling139533
85SuttonViolence Against the Person225409
86SuttonCriminal Damage314474
87Tower HamletsTheft and Handling187620
88Tower HamletsViolence Against the Person259993
89Tower HamletsDrugs323408
90Waltham ForestTheft and Handling177940
91Waltham ForestViolence Against the Person251898
92Waltham ForestBurglary325565
93WandsworthTheft and Handling192523
94WandsworthViolence Against the Person245865
95WandsworthBurglary325533
96WestminsterTheft and Handling1277617
97WestminsterViolence Against the Person271448
98WestminsterDrugs334031
london = bq_helper.BigQueryHelper(active_project="bigquery-public-data", dataset_name="london_crime")
top_crimes_borough = london.query_to_pandas_safe(top_crimes_borough_query)
var = top_crimes_borough.groupby(['borough','major_category']).sum()
df = var.groupby('borough').apply(lambda x: x.nlargest(3,'total_crimes')).reset_index(level=0, drop=True)
total_crimes
boroughmajor_category
Barking and DagenhamViolence Against the Person6067
Theft and Handling5607
Criminal Damage1949
BarnetTheft and Handling9731
Violence Against the Person7499
Burglary3402
BexleyViolence Against the Person4503
Theft and Handling4392
Criminal Damage1673
BrentViolence Against the Person9205
Theft and Handling9026
Burglary2631
BromleyTheft and Handling7584
Violence Against the Person6650
Burglary2214
CamdenTheft and Handling14088
Violence Against the Person7626
Burglary2652
City of LondonTheft and Handling129
Violence Against the Person25
Drugs10
CroydonViolence Against the Person10302
Theft and Handling9229
Criminal Damage3219
EalingTheft and Handling10040
Violence Against the Person9396
Criminal Damage2562
EnfieldTheft and Handling8037
Violence Against the Person7409
Burglary2541
GreenwichViolence Against the Person8590
Theft and Handling8010
Criminal Damage2476
HackneyTheft and Handling11851
Violence Against the Person8832
Burglary2719
Hammersmith and FulhamTheft and Handling8925
Violence Against the Person6118
Burglary1531
HaringeyTheft and Handling10554
Violence Against the Person8574
Burglary2474
HarrowTheft and Handling4537
Violence Against the Person4293
Burglary1994
HaveringViolence Against the Person5936
Theft and Handling5919
Burglary1826
HillingdonTheft and Handling9045
Violence Against the Person7830
Criminal Damage2470
HounslowTheft and Handling8682
Violence Against the Person7405
Criminal Damage2213
IslingtonTheft and Handling12077
Violence Against the Person7840
Burglary2051
Kensington and ChelseaTheft and Handling9855
Violence Against the Person4696
Drugs1680
Kingston upon ThamesTheft and Handling3803
Violence Against the Person3194
Criminal Damage1054
LambethTheft and Handling13155
Violence Against the Person10496
Burglary3087
LewishamViolence Against the Person8809
Theft and Handling7702
Criminal Damage2351
MertonTheft and Handling4894
Violence Against the Person4026
Burglary1419
NewhamTheft and Handling11964
Violence Against the Person9646
Criminal Damage2496
RedbridgeTheft and Handling7447
Violence Against the Person6411
Burglary1997
Richmond upon ThamesTheft and Handling4769
Violence Against the Person3155
Burglary1359
SouthwarkTheft and Handling12946
Violence Against the Person9474
Burglary2946
SuttonViolence Against the Person3714
Theft and Handling3516
Criminal Damage1316
Tower HamletsTheft and Handling10953
Violence Against the Person9608
Burglary2794
Waltham ForestViolence Against the Person7409
Theft and Handling7357
Criminal Damage1989
WandsworthTheft and Handling10789
Violence Against the Person6809
Burglary2351
WestminsterTheft and Handling27520
Violence Against the Person10834
Burglary3218
df.plot(kind='barh', figsize=(10,100))

In [101]:

top_crimes_borough.groupby(['borough','major_category']).sum().plot(kind='barh', figsize=(10,100))
%%bigquery
SELECT borough, major_category, SUM(value) AS total_crimes
FROM bigquery-public-data.london_crime.crime_by_lsoa
WHERE borough = 'Barking and Dagenham'
GROUP BY borough, major_category
ORDER BY borough, total_crimes DESC
LIMIT 3
Query complete after 0.00s: 100%|███████████████████████████████████████████████████| 1/1 [00:00<00:00, 1008.00query/s]
Downloading: 100%|█████████████████████████████████████████████████████████████████████| 3/3 [00:00<00:00,  3.06rows/s]
boroughmajor_categorytotal_crimes
0Barking and DagenhamTheft and Handling50999
1Barking and DagenhamViolence Against the Person43091
2Barking and DagenhamCriminal Damage18888
%%bigquery
SELECT
  bigquery-public-data.london_crime.crime_by_lsoa.*
FROM
  bigquery-public-data.london_crime.crime_by_lsoa INNER JOIN (
    SELECT
      borough,
      GROUP_CONCAT(year ORDER BY total_crimes DESC) grouped_year
    FROM
      yourtable
    GROUP BY id) group_max
  ON yourtable.id = group_max.id
     AND FIND_IN_SET(year, grouped_year) BETWEEN 1 AND 5
ORDER BY
  yourtable.id, yourtable.year DESC;

In [8]:

%%bigquery
SELECT SUM(value) AS total_crimes, major_category, year 
FROM bigquery-public-data.london_crime.crime_by_lsoa
WHERE year between 2011 and 2016
GROUP BY year
ORDER BY year
Executing query with job ID: 7eac20b8-460d-432d-8cec-b6c422ea8f7a
Query executing: 0.44s

References

https://cloud.google.com/docs/authentication/getting-started
https://cloud.google.com/bigquery/docs/reference/libraries#windows
https://stackoverflow.com/questions/37400974/unicode-error-unicodeescape-codec-cant-decode-bytes-in-position-2-3-trunca
https://stackoverflow.com/questions/45501082/set-google-application-credentials-in-python-project-to-use-google-api

Author

Write A Comment