Tải tệp json lớn trong python

Trong quá trình thử nghiệm của mình, tôi đã phải đối mặt với điều đó khi Python tải một tệp JSON vào bộ nhớ của nó, nó thậm chí còn lớn gấp 10 lần so với trọng lượng của tệp trên FS

Để đưa ra các giá trị, kích thước JSON là khoảng 300 MB, nhưng tổng bộ nhớ được sử dụng bởi ứng dụng Python sau khi phân tích cú pháp JSON là khoảng 2. 8GB

Không thể tưởng tượng được việc duy trì cơ sở hạ tầng này cho một ứng dụng duy nhất

Vâng, có thể Python không phải là công cụ chính xác để làm điều này, nhưng nó cần thiết, bởi vì những thông tin đó phải được sử dụng với các thư viện chỉ có trong Python

Tôi muốn chắc chắn rằng mình đã nhầm lẫn, vì vậy tôi đã thử nghiệm với một tệp JSON nhỏ hơn và so sánh nó với JS, chạy trên môi trường NodeJS

Kích thước của tệp json trên hệ thống tệp là 18,6 MB

Hãy xem Python chiếm bao nhiêu bộ nhớ

Trong cốt truyện này, Thứ Hai là

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
16 và Chủ nhật là
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
17. Có vẻ như Chủ Nhật có nhiều điểm dừng nhất và Thứ Hai có ít điểm dừng nhất. Đây cũng có thể là vấn đề về chất lượng dữ liệu khi ngày không hợp lệ dẫn đến Chủ nhật vì một số lý do. Bạn sẽ phải đào sâu hơn vào cột
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
15 để tìm ra nó một cách dứt khoát [điều này nằm ngoài phạm vi của bài đăng này]

Chúng tôi cũng có thể vạch ra thời gian dừng giao thông phổ biến nhất

%%bashhead md_traffic.json
6_______5_______7

Có vẻ như nhiều điểm dừng nhất xảy ra vào khoảng nửa đêm và ít điểm dừng nhất xảy ra vào khoảng 5 giờ sáng. Điều này có thể hợp lý, vì mọi người đang lái xe về nhà từ quán bar và bữa tối muộn và đêm, và có thể bị suy giảm. Đây cũng có thể là vấn đề về chất lượng dữ liệu và cần xem qua cột

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
14 để có câu trả lời đầy đủ

Đặt lại các điểm dừng

Bây giờ chúng tôi đã chuyển đổi các cột vị trí và ngày, chúng tôi có thể vạch ra các điểm dừng giao thông. Vì ánh xạ rất tốn tài nguyên CPU và bộ nhớ, trước tiên, chúng tôi cần lọc xuống các hàng mà chúng tôi sử dụng từ

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
20

%%bashhead md_traffic.json
8

Trong đoạn mã trên, chúng tôi đã chọn tất cả các hàng xuất hiện trong năm qua. Chúng tôi có thể thu hẹp phạm vi này hơn nữa và chỉ chọn các hàng xảy ra trong giờ cao điểm — khoảng thời gian buổi sáng khi mọi người đi làm

%%bashhead md_traffic.json
9
{ "meta" : {
"view" : {
"id" : "4mse-ku6q",
"name" : "Traffic Violations",
"averageRating" : 0,
"category" : "Public Safety",
"createdAt" : 1403103517,
"description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily",
"displayType" : "table",
0
{ "meta" : {
"view" : {
"id" : "4mse-ku6q",
"name" : "Traffic Violations",
"averageRating" : 0,
"category" : "Public Safety",
"createdAt" : 1403103517,
"description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily",
"displayType" : "table",
1

Sử dụng gói folium tuyệt vời, giờ đây chúng ta có thể hình dung nơi tất cả các điểm dừng xảy ra. Folium cho phép bạn dễ dàng tạo bản đồ tương tác bằng Python bằng cách tận dụng tờ rơi. Để duy trì hiệu suất, chúng tôi sẽ chỉ hình dung

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
21 hàng đầu tiên của
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
22

{ "meta" : {
"view" : {
"id" : "4mse-ku6q",
"name" : "Traffic Violations",
"averageRating" : 0,
"category" : "Public Safety",
"createdAt" : 1403103517,
"description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily",
"displayType" : "table",
2

Điều này cho thấy nhiều điểm dừng giao thông tập trung quanh phía dưới bên phải của quận. Chúng tôi có thể mở rộng phân tích của mình hơn nữa bằng bản đồ nhiệt

{ "meta" : {
"view" : {
"id" : "4mse-ku6q",
"name" : "Traffic Violations",
"averageRating" : 0,
"category" : "Public Safety",
"createdAt" : 1403103517,
"description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily",
"displayType" : "table",
3

Làm việc với các bộ dữ liệu JSON lớn có thể gây khó khăn, đặc biệt khi chúng quá lớn để vừa với bộ nhớ. Trong những trường hợp như thế này, sự kết hợp giữa các công cụ dòng lệnh và Python có thể tạo ra một cách hiệu quả để khám phá và phân tích dữ liệu. Trong bài đăng này, tập trung vào việc học lập trình python, chúng ta sẽ xem xét cách tận dụng các công cụ như Pandas để khám phá và vạch ra hoạt động của cảnh sát ở Quận Montgomery, Maryland. Chúng ta sẽ bắt đầu bằng việc xem xét dữ liệu JSON, sau đó chuyển sang khám phá và phân tích JSON bằng Python

Khi dữ liệu được lưu trữ trong cơ sở dữ liệu SQL, nó có xu hướng tuân theo một cấu trúc cứng nhắc trông giống như một bảng. Đây là một ví dụ từ cơ sở dữ liệu SQLite

id|code|name|area|area_land|area_water|population|population_growth|birth_rate|death_rate|migration_rate|created_at|updated_at1|af|Afghanistan|652230|652230|0|32564342|2.32|38.57|13.89|1.51|2015-11-01 13:19:49.461734|2015-11-01 13:19:49.4617342|al|Albania|28748|27398|1350|3029278|0.3|12.92|6.58|3.3|2015-11-01 13:19:54.431082|2015-11-01 13:19:54.4310823|ag|Algeria|2381741|2381741|0|39542166|1.84|23.67|4.31|0.92|2015-11-01 13:19:59.961286|2015-11-01 13:19:59.961286

Như bạn có thể thấy, dữ liệu bao gồm các hàng và cột, trong đó mỗi cột ánh xạ tới một thuộc tính đã xác định, chẳng hạn như

[ [1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050"], [1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050"], ...]
8 hoặc
[ [1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050"], [1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050"], ...]
9. Trong tập dữ liệu ở trên, mỗi hàng đại diện cho một quốc gia và mỗi cột đại diện cho một số dữ kiện về quốc gia đó

Nhưng khi lượng dữ liệu chúng tôi thu thập tăng lên, chúng tôi thường không biết cấu trúc chính xác của dữ liệu tại thời điểm chúng tôi lưu trữ dữ liệu đó. Đây được gọi là dữ liệu phi cấu trúc. Một ví dụ điển hình là danh sách các sự kiện từ khách truy cập trên một trang web. Đây là một ví dụ về danh sách các sự kiện được gửi đến máy chủ

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},

Như bạn có thể thấy, ba sự kiện riêng biệt được liệt kê ở trên. Mỗi sự kiện có các trường khác nhau và một số trường được lồng trong các trường khác. Loại dữ liệu này rất khó lưu trữ trong cơ sở dữ liệu SQL thông thường. Dữ liệu phi cấu trúc này thường được lưu trữ ở định dạng có tên là Ký hiệu đối tượng JavaScript [JSON]. JSON là một cách để mã hóa các cấu trúc dữ liệu như danh sách và từ điển thành các chuỗi để đảm bảo rằng chúng có thể dễ dàng đọc được bằng máy. Mặc dù JSON bắt đầu bằng từ Javascript, nhưng nó thực sự chỉ là một định dạng và có thể được đọc bởi bất kỳ ngôn ngữ nào

Python có hỗ trợ JSON tuyệt vời, với thư viện json. Chúng tôi có thể vừa chuyển đổi danh sách và từ điển thành JSON, vừa chuyển đổi chuỗi thành danh sách và từ điển. Dữ liệu JSON trông giống như một từ điển trong Python, với các khóa và giá trị được lưu trữ

Trong bài đăng này, chúng ta sẽ khám phá một tệp JSON trên dòng lệnh, sau đó nhập nó vào Python và làm việc với nó bằng Pandas

tập dữ liệu

Chúng tôi sẽ xem xét tập dữ liệu chứa thông tin về vi phạm giao thông ở Quận Montgomery, Maryland. Bạn có thể tải dữ liệu tại đây. Dữ liệu chứa thông tin về nơi xảy ra vi phạm, loại ô tô, nhân khẩu học của người nhận vi phạm và một số thông tin thú vị khác. Có khá nhiều câu hỏi chúng tôi có thể trả lời bằng cách sử dụng bộ dữ liệu này, bao gồm

  • Những loại xe nào có nhiều khả năng bị kéo vì chạy quá tốc độ?
  • Thời gian nào trong ngày cảnh sát hoạt động tích cực nhất?
  • “Bẫy tốc độ” phổ biến như thế nào?
  • Những điều phổ biến nhất mà mọi người bị kéo qua là gì?

Thật không may, chúng tôi không biết trước cấu trúc của tệp JSON, vì vậy chúng tôi sẽ cần thực hiện một số khám phá để tìm ra nó. Chúng tôi sẽ sử dụng Jupyter Notebook cho khám phá này

Khám phá dữ liệu JSON

Mặc dù tệp JSON chỉ có dung lượng 600MB nhưng chúng tôi sẽ xử lý tệp như thể nó lớn hơn nhiều để chúng tôi có thể khám phá cách phân tích tệp JSON không vừa với bộ nhớ có thể hoạt động như thế nào. Điều đầu tiên chúng ta sẽ làm là xem một vài dòng đầu tiên của tệp

%%bashgrep -E '^ {2,6}"' md_traffic.json
0. Tệp JSON chỉ là một tệp văn bản thông thường, vì vậy chúng tôi có thể sử dụng tất cả các công cụ dòng lệnh tiêu chuẩn để tương tác với nó

%%bashhead md_traffic.json
{ "meta" : {
"view" : {
"id" : "4mse-ku6q",
"name" : "Traffic Violations",
"averageRating" : 0,
"category" : "Public Safety",
"createdAt" : 1403103517,
"description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily",
"displayType" : "table",

Từ đó, chúng ta có thể biết rằng dữ liệu JSON là một từ điển và được định dạng tốt.

%%bashgrep -E '^ {2,6}"' md_traffic.json
1 là khóa cấp cao nhất và được thụt vào hai dấu cách. Chúng ta có thể lấy tất cả các khóa cấp cao nhất bằng cách sử dụng lệnh grep để in bất kỳ dòng nào có hai khoảng trắng ở đầu

%%bashgrep -E '^ {2}"' md_traffic.json
"meta" : {"data" : [
[ 1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050", 1455876689, "498050", null, "2016-02-18T00:00:00", "09:05:00", "MCP", "2nd district, Bethesda", "DRIVER USING HANDS TO USE HANDHELD TELEPHONE WHILEMOTOR VEHICLE IS IN MOTION", "355/TUCKERMAN LN", "-77.105925", "39.03223", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "MD", "02 - Automobile", "2010", "JEEP", "CRISWELL", "BLUE", "Citation", "21-1124.2[d2]", "Transportation Article", "No", "WHITE", "F", "GERMANTOWN", "MD", "MD", "A - Marked Patrol", [ "{\"address\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\"}", "-77.105925", "39.03223", null, false ] ]

Điều này cho chúng ta thấy rằng

%%bashgrep -E '^ {2,6}"' md_traffic.json
1 và
%%bashgrep -E '^ {2,6}"' md_traffic.json
3 là các khóa cấp cao nhất trong dữ liệu
%%bashgrep -E '^ {2,6}"' md_traffic.json
0. Một danh sách các danh sách dường như được liên kết với
%%bashgrep -E '^ {2,6}"' md_traffic.json
3 và danh sách này có thể chứa từng bản ghi trong bộ dữ liệu vi phạm giao thông của chúng tôi. Mỗi danh sách bên trong là một bản ghi và bản ghi đầu tiên xuất hiện trong đầu ra từ lệnh
%%bashgrep -E '^ {2,6}"' md_traffic.json
6. Điều này rất giống với loại dữ liệu có cấu trúc mà chúng ta thường làm việc khi thao tác trên tệp CSV hoặc bảng SQL. Đây là chế độ xem bị cắt bớt về cách dữ liệu có thể trông như thế nào

[ [1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050"], [1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050"], ...]

Điều này trông rất giống với các hàng và cột mà chúng ta thường làm việc với. Chúng tôi chỉ thiếu các tiêu đề cho chúng tôi biết ý nghĩa của từng cột. Chúng tôi có thể tìm thấy thông tin này theo khóa

%%bashgrep -E '^ {2,6}"' md_traffic.json
1

%%bashgrep -E '^ {2,6}"' md_traffic.json
1 thường đề cập đến thông tin về chính dữ liệu. Hãy tìm hiểu thêm một chút về
%%bashgrep -E '^ {2,6}"' md_traffic.json
1 và xem thông tin nào được chứa trong đó. Từ lệnh
"meta" : { "view" : { "id" : "4mse-ku6q", "name" : "Traffic Violations", "averageRating" : 0, "category" : "Public Safety", "createdAt" : 1403103517, "description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily", "displayType" : "table", "downloadCount" : 2851, "iconUrl" : "fileId:r41tDc239M1FL75LFwXFKzFCWqr8mzMeMTYXiA24USM", "indexUpdatedAt" : 1455885508, "newBackend" : false, "numberOfComments" : 0, "oid" : 8890705, "publicationAppendEnabled" : false, "publicationDate" : 1411040702, "publicationGroup" : 1620779, "publicationStage" : "published", "rowClass" : "", "rowsUpdatedAt" : 1455876727, "rowsUpdatedBy" : "ajn4-zy65", "state" : "normal", "tableId" : 1722160, "totalTimesRated" : 0, "viewCount" : 6910, "viewLastModified" : 1434558076, "viewType" : "tabular", "columns" : [ { "disabledFeatureFlags" : [ "allow_comments" ], "grants" : [ { "metadata" : { "owner" : { "query" : { "rights" : [ "read" ], "sortBys" : [ { "tableAuthor" : { "tags" : [ "traffic", "stop", "violations", "electronic issued." ], "flags" : [ "default" ]"data" : [ [ 1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050", 1455876689, "498050", null, "2016-02-18T00:00:00", "09:05:00", "MCP", "2nd district, Bethesda", "DRIVER USING HANDS TO USE HANDHELD TELEPHONE WHILEMOTOR VEHICLE IS IN MOTION", "355/TUCKERMAN LN", "-77.105925", "39.03223", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "MD", "02 - Automobile", "2010", "JEEP", "CRISWELL", "BLUE", "Citation", "21-1124.2[d2]", "Transportation Article", "No", "WHITE", "F", "GERMANTOWN", "MD", "MD", "A - Marked Patrol", [ "{\"address\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\"}", "-77.105925", "39.03223", null, false ] ]
0, chúng tôi biết rằng có ít nhất
"meta" : { "view" : { "id" : "4mse-ku6q", "name" : "Traffic Violations", "averageRating" : 0, "category" : "Public Safety", "createdAt" : 1403103517, "description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily", "displayType" : "table", "downloadCount" : 2851, "iconUrl" : "fileId:r41tDc239M1FL75LFwXFKzFCWqr8mzMeMTYXiA24USM", "indexUpdatedAt" : 1455885508, "newBackend" : false, "numberOfComments" : 0, "oid" : 8890705, "publicationAppendEnabled" : false, "publicationDate" : 1411040702, "publicationGroup" : 1620779, "publicationStage" : "published", "rowClass" : "", "rowsUpdatedAt" : 1455876727, "rowsUpdatedBy" : "ajn4-zy65", "state" : "normal", "tableId" : 1722160, "totalTimesRated" : 0, "viewCount" : 6910, "viewLastModified" : 1434558076, "viewType" : "tabular", "columns" : [ { "disabledFeatureFlags" : [ "allow_comments" ], "grants" : [ { "metadata" : { "owner" : { "query" : { "rights" : [ "read" ], "sortBys" : [ { "tableAuthor" : { "tags" : [ "traffic", "stop", "violations", "electronic issued." ], "flags" : [ "default" ]"data" : [ [ 1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050", 1455876689, "498050", null, "2016-02-18T00:00:00", "09:05:00", "MCP", "2nd district, Bethesda", "DRIVER USING HANDS TO USE HANDHELD TELEPHONE WHILEMOTOR VEHICLE IS IN MOTION", "355/TUCKERMAN LN", "-77.105925", "39.03223", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "MD", "02 - Automobile", "2010", "JEEP", "CRISWELL", "BLUE", "Citation", "21-1124.2[d2]", "Transportation Article", "No", "WHITE", "F", "GERMANTOWN", "MD", "MD", "A - Marked Patrol", [ "{\"address\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\"}", "-77.105925", "39.03223", null, false ] ]
1 cấp khóa, với
%%bashgrep -E '^ {2,6}"' md_traffic.json
1 chứa khóa
"meta" : { "view" : { "id" : "4mse-ku6q", "name" : "Traffic Violations", "averageRating" : 0, "category" : "Public Safety", "createdAt" : 1403103517, "description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily", "displayType" : "table", "downloadCount" : 2851, "iconUrl" : "fileId:r41tDc239M1FL75LFwXFKzFCWqr8mzMeMTYXiA24USM", "indexUpdatedAt" : 1455885508, "newBackend" : false, "numberOfComments" : 0, "oid" : 8890705, "publicationAppendEnabled" : false, "publicationDate" : 1411040702, "publicationGroup" : 1620779, "publicationStage" : "published", "rowClass" : "", "rowsUpdatedAt" : 1455876727, "rowsUpdatedBy" : "ajn4-zy65", "state" : "normal", "tableId" : 1722160, "totalTimesRated" : 0, "viewCount" : 6910, "viewLastModified" : 1434558076, "viewType" : "tabular", "columns" : [ { "disabledFeatureFlags" : [ "allow_comments" ], "grants" : [ { "metadata" : { "owner" : { "query" : { "rights" : [ "read" ], "sortBys" : [ { "tableAuthor" : { "tags" : [ "traffic", "stop", "violations", "electronic issued." ], "flags" : [ "default" ]"data" : [ [ 1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050", 1455876689, "498050", null, "2016-02-18T00:00:00", "09:05:00", "MCP", "2nd district, Bethesda", "DRIVER USING HANDS TO USE HANDHELD TELEPHONE WHILEMOTOR VEHICLE IS IN MOTION", "355/TUCKERMAN LN", "-77.105925", "39.03223", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "MD", "02 - Automobile", "2010", "JEEP", "CRISWELL", "BLUE", "Citation", "21-1124.2[d2]", "Transportation Article", "No", "WHITE", "F", "GERMANTOWN", "MD", "MD", "A - Marked Patrol", [ "{\"address\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\"}", "-77.105925", "39.03223", null, false ] ]
3, cấp này chứa khóa
[ [1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050"], [1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050"], ...]
8,
"meta" : { "view" : { "id" : "4mse-ku6q", "name" : "Traffic Violations", "averageRating" : 0, "category" : "Public Safety", "createdAt" : 1403103517, "description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily", "displayType" : "table", "downloadCount" : 2851, "iconUrl" : "fileId:r41tDc239M1FL75LFwXFKzFCWqr8mzMeMTYXiA24USM", "indexUpdatedAt" : 1455885508, "newBackend" : false, "numberOfComments" : 0, "oid" : 8890705, "publicationAppendEnabled" : false, "publicationDate" : 1411040702, "publicationGroup" : 1620779, "publicationStage" : "published", "rowClass" : "", "rowsUpdatedAt" : 1455876727, "rowsUpdatedBy" : "ajn4-zy65", "state" : "normal", "tableId" : 1722160, "totalTimesRated" : 0, "viewCount" : 6910, "viewLastModified" : 1434558076, "viewType" : "tabular", "columns" : [ { "disabledFeatureFlags" : [ "allow_comments" ], "grants" : [ { "metadata" : { "owner" : { "query" : { "rights" : [ "read" ], "sortBys" : [ { "tableAuthor" : { "tags" : [ "traffic", "stop", "violations", "electronic issued." ], "flags" : [ "default" ]"data" : [ [ 1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050", 1455876689, "498050", null, "2016-02-18T00:00:00", "09:05:00", "MCP", "2nd district, Bethesda", "DRIVER USING HANDS TO USE HANDHELD TELEPHONE WHILEMOTOR VEHICLE IS IN MOTION", "355/TUCKERMAN LN", "-77.105925", "39.03223", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "MD", "02 - Automobile", "2010", "JEEP", "CRISWELL", "BLUE", "Citation", "21-1124.2[d2]", "Transportation Article", "No", "WHITE", "F", "GERMANTOWN", "MD", "MD", "A - Marked Patrol", [ "{\"address\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\"}", "-77.105925", "39.03223", null, false ] ]
5,
"meta" : { "view" : { "id" : "4mse-ku6q", "name" : "Traffic Violations", "averageRating" : 0, "category" : "Public Safety", "createdAt" : 1403103517, "description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily", "displayType" : "table", "downloadCount" : 2851, "iconUrl" : "fileId:r41tDc239M1FL75LFwXFKzFCWqr8mzMeMTYXiA24USM", "indexUpdatedAt" : 1455885508, "newBackend" : false, "numberOfComments" : 0, "oid" : 8890705, "publicationAppendEnabled" : false, "publicationDate" : 1411040702, "publicationGroup" : 1620779, "publicationStage" : "published", "rowClass" : "", "rowsUpdatedAt" : 1455876727, "rowsUpdatedBy" : "ajn4-zy65", "state" : "normal", "tableId" : 1722160, "totalTimesRated" : 0, "viewCount" : 6910, "viewLastModified" : 1434558076, "viewType" : "tabular", "columns" : [ { "disabledFeatureFlags" : [ "allow_comments" ], "grants" : [ { "metadata" : { "owner" : { "query" : { "rights" : [ "read" ], "sortBys" : [ { "tableAuthor" : { "tags" : [ "traffic", "stop", "violations", "electronic issued." ], "flags" : [ "default" ]"data" : [ [ 1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050", 1455876689, "498050", null, "2016-02-18T00:00:00", "09:05:00", "MCP", "2nd district, Bethesda", "DRIVER USING HANDS TO USE HANDHELD TELEPHONE WHILEMOTOR VEHICLE IS IN MOTION", "355/TUCKERMAN LN", "-77.105925", "39.03223", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "MD", "02 - Automobile", "2010", "JEEP", "CRISWELL", "BLUE", "Citation", "21-1124.2[d2]", "Transportation Article", "No", "WHITE", "F", "GERMANTOWN", "MD", "MD", "A - Marked Patrol", [ "{\"address\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\"}", "-77.105925", "39.03223", null, false ] ]
6 và các khóa khác. Chúng ta có thể in ra cấu trúc khóa đầy đủ của tệp JSON bằng cách sử dụng grep để in ra bất kỳ dòng nào có khoảng trắng ở đầu
"meta" : { "view" : { "id" : "4mse-ku6q", "name" : "Traffic Violations", "averageRating" : 0, "category" : "Public Safety", "createdAt" : 1403103517, "description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily", "displayType" : "table", "downloadCount" : 2851, "iconUrl" : "fileId:r41tDc239M1FL75LFwXFKzFCWqr8mzMeMTYXiA24USM", "indexUpdatedAt" : 1455885508, "newBackend" : false, "numberOfComments" : 0, "oid" : 8890705, "publicationAppendEnabled" : false, "publicationDate" : 1411040702, "publicationGroup" : 1620779, "publicationStage" : "published", "rowClass" : "", "rowsUpdatedAt" : 1455876727, "rowsUpdatedBy" : "ajn4-zy65", "state" : "normal", "tableId" : 1722160, "totalTimesRated" : 0, "viewCount" : 6910, "viewLastModified" : 1434558076, "viewType" : "tabular", "columns" : [ { "disabledFeatureFlags" : [ "allow_comments" ], "grants" : [ { "metadata" : { "owner" : { "query" : { "rights" : [ "read" ], "sortBys" : [ { "tableAuthor" : { "tags" : [ "traffic", "stop", "violations", "electronic issued." ], "flags" : [ "default" ]"data" : [ [ 1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050", 1455876689, "498050", null, "2016-02-18T00:00:00", "09:05:00", "MCP", "2nd district, Bethesda", "DRIVER USING HANDS TO USE HANDHELD TELEPHONE WHILEMOTOR VEHICLE IS IN MOTION", "355/TUCKERMAN LN", "-77.105925", "39.03223", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "MD", "02 - Automobile", "2010", "JEEP", "CRISWELL", "BLUE", "Citation", "21-1124.2[d2]", "Transportation Article", "No", "WHITE", "F", "GERMANTOWN", "MD", "MD", "A - Marked Patrol", [ "{\"address\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\"}", "-77.105925", "39.03223", null, false ] ]
7

%%bashgrep -E '^ {2,6}"' md_traffic.json
"meta" : { "view" : { "id" : "4mse-ku6q", "name" : "Traffic Violations", "averageRating" : 0, "category" : "Public Safety", "createdAt" : 1403103517, "description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily", "displayType" : "table", "downloadCount" : 2851, "iconUrl" : "fileId:r41tDc239M1FL75LFwXFKzFCWqr8mzMeMTYXiA24USM", "indexUpdatedAt" : 1455885508, "newBackend" : false, "numberOfComments" : 0, "oid" : 8890705, "publicationAppendEnabled" : false, "publicationDate" : 1411040702, "publicationGroup" : 1620779, "publicationStage" : "published", "rowClass" : "", "rowsUpdatedAt" : 1455876727, "rowsUpdatedBy" : "ajn4-zy65", "state" : "normal", "tableId" : 1722160, "totalTimesRated" : 0, "viewCount" : 6910, "viewLastModified" : 1434558076, "viewType" : "tabular", "columns" : [ { "disabledFeatureFlags" : [ "allow_comments" ], "grants" : [ { "metadata" : { "owner" : { "query" : { "rights" : [ "read" ], "sortBys" : [ { "tableAuthor" : { "tags" : [ "traffic", "stop", "violations", "electronic issued." ], "flags" : [ "default" ]"data" : [ [ 1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050", 1455876689, "498050", null, "2016-02-18T00:00:00", "09:05:00", "MCP", "2nd district, Bethesda", "DRIVER USING HANDS TO USE HANDHELD TELEPHONE WHILEMOTOR VEHICLE IS IN MOTION", "355/TUCKERMAN LN", "-77.105925", "39.03223", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "MD", "02 - Automobile", "2010", "JEEP", "CRISWELL", "BLUE", "Citation", "21-1124.2[d2]", "Transportation Article", "No", "WHITE", "F", "GERMANTOWN", "MD", "MD", "A - Marked Patrol", [ "{\"address\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\"}", "-77.105925", "39.03223", null, false ] ]

Điều này cho chúng tôi thấy cấu trúc khóa đầy đủ được liên kết với

%%bashgrep -E '^ {2,6}"' md_traffic.json
0 và cho chúng tôi biết phần nào của tệp JSON có liên quan đến chúng tôi. Trong trường hợp này, khóa
"meta" : { "view" : { "id" : "4mse-ku6q", "name" : "Traffic Violations", "averageRating" : 0, "category" : "Public Safety", "createdAt" : 1403103517, "description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily", "displayType" : "table", "downloadCount" : 2851, "iconUrl" : "fileId:r41tDc239M1FL75LFwXFKzFCWqr8mzMeMTYXiA24USM", "indexUpdatedAt" : 1455885508, "newBackend" : false, "numberOfComments" : 0, "oid" : 8890705, "publicationAppendEnabled" : false, "publicationDate" : 1411040702, "publicationGroup" : 1620779, "publicationStage" : "published", "rowClass" : "", "rowsUpdatedAt" : 1455876727, "rowsUpdatedBy" : "ajn4-zy65", "state" : "normal", "tableId" : 1722160, "totalTimesRated" : 0, "viewCount" : 6910, "viewLastModified" : 1434558076, "viewType" : "tabular", "columns" : [ { "disabledFeatureFlags" : [ "allow_comments" ], "grants" : [ { "metadata" : { "owner" : { "query" : { "rights" : [ "read" ], "sortBys" : [ { "tableAuthor" : { "tags" : [ "traffic", "stop", "violations", "electronic issued." ], "flags" : [ "default" ]"data" : [ [ 1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050", 1455876689, "498050", null, "2016-02-18T00:00:00", "09:05:00", "MCP", "2nd district, Bethesda", "DRIVER USING HANDS TO USE HANDHELD TELEPHONE WHILEMOTOR VEHICLE IS IN MOTION", "355/TUCKERMAN LN", "-77.105925", "39.03223", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "MD", "02 - Automobile", "2010", "JEEP", "CRISWELL", "BLUE", "Citation", "21-1124.2[d2]", "Transportation Article", "No", "WHITE", "F", "GERMANTOWN", "MD", "MD", "A - Marked Patrol", [ "{\"address\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\"}", "-77.105925", "39.03223", null, false ] ]
9 có vẻ thú vị, vì nó có khả năng chứa thông tin về các cột trong danh sách các danh sách trong khóa
%%bashgrep -E '^ {2,6}"' md_traffic.json
3

Trích xuất thông tin trên các cột

Bây giờ chúng ta đã biết khóa nào chứa thông tin trên các cột, chúng ta cần đọc thông tin đó trong. Vì chúng tôi cho rằng tệp JSON sẽ không vừa trong bộ nhớ, nên chúng tôi không thể đọc trực tiếp tệp đó bằng thư viện json. Thay vào đó, chúng ta sẽ cần đọc lặp đi lặp lại nó theo cách tiết kiệm bộ nhớ

Chúng ta có thể thực hiện việc này bằng cách sử dụng gói ijson. ijson sẽ lặp lại phân tích cú pháp tệp json thay vì đọc tất cả cùng một lúc. Điều này chậm hơn so với đọc trực tiếp toàn bộ tệp, nhưng nó cho phép chúng tôi làm việc với các tệp lớn không vừa với bộ nhớ. Để sử dụng ijson, chúng tôi chỉ định một tệp mà chúng tôi muốn trích xuất dữ liệu từ đó, sau đó chúng tôi chỉ định một đường dẫn chính để trích xuất

import ijson
filename = "md_traffic.json"
with open[filename, 'r'] as f:
    objects = ijson.items[f, 'meta.view.columns.item']
    columns = list[objects]

Trong đoạn mã trên, chúng tôi mở tệp

%%bashgrep -E '^ {2,6}"' md_traffic.json
0, sau đó chúng tôi sử dụng phương thức
import ijson
filename = "md_traffic.json"
with open[filename, 'r'] as f:
    objects = ijson.items[f, 'meta.view.columns.item']
    columns = list[objects]
2 trong ijson để trích xuất một danh sách từ tệp. Chúng tôi chỉ định đường dẫn đến danh sách bằng cách sử dụng ký hiệu
import ijson
filename = "md_traffic.json"
with open[filename, 'r'] as f:
    objects = ijson.items[f, 'meta.view.columns.item']
    columns = list[objects]
3. Nhớ lại rằng
%%bashgrep -E '^ {2,6}"' md_traffic.json
1 là khóa cấp cao nhất, bên trong chứa
"meta" : { "view" : { "id" : "4mse-ku6q", "name" : "Traffic Violations", "averageRating" : 0, "category" : "Public Safety", "createdAt" : 1403103517, "description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily", "displayType" : "table", "downloadCount" : 2851, "iconUrl" : "fileId:r41tDc239M1FL75LFwXFKzFCWqr8mzMeMTYXiA24USM", "indexUpdatedAt" : 1455885508, "newBackend" : false, "numberOfComments" : 0, "oid" : 8890705, "publicationAppendEnabled" : false, "publicationDate" : 1411040702, "publicationGroup" : 1620779, "publicationStage" : "published", "rowClass" : "", "rowsUpdatedAt" : 1455876727, "rowsUpdatedBy" : "ajn4-zy65", "state" : "normal", "tableId" : 1722160, "totalTimesRated" : 0, "viewCount" : 6910, "viewLastModified" : 1434558076, "viewType" : "tabular", "columns" : [ { "disabledFeatureFlags" : [ "allow_comments" ], "grants" : [ { "metadata" : { "owner" : { "query" : { "rights" : [ "read" ], "sortBys" : [ { "tableAuthor" : { "tags" : [ "traffic", "stop", "violations", "electronic issued." ], "flags" : [ "default" ]"data" : [ [ 1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050", 1455876689, "498050", null, "2016-02-18T00:00:00", "09:05:00", "MCP", "2nd district, Bethesda", "DRIVER USING HANDS TO USE HANDHELD TELEPHONE WHILEMOTOR VEHICLE IS IN MOTION", "355/TUCKERMAN LN", "-77.105925", "39.03223", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "MD", "02 - Automobile", "2010", "JEEP", "CRISWELL", "BLUE", "Citation", "21-1124.2[d2]", "Transportation Article", "No", "WHITE", "F", "GERMANTOWN", "MD", "MD", "A - Marked Patrol", [ "{\"address\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\"}", "-77.105925", "39.03223", null, false ] ]
3, bên trong chứa
"meta" : { "view" : { "id" : "4mse-ku6q", "name" : "Traffic Violations", "averageRating" : 0, "category" : "Public Safety", "createdAt" : 1403103517, "description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily", "displayType" : "table", "downloadCount" : 2851, "iconUrl" : "fileId:r41tDc239M1FL75LFwXFKzFCWqr8mzMeMTYXiA24USM", "indexUpdatedAt" : 1455885508, "newBackend" : false, "numberOfComments" : 0, "oid" : 8890705, "publicationAppendEnabled" : false, "publicationDate" : 1411040702, "publicationGroup" : 1620779, "publicationStage" : "published", "rowClass" : "", "rowsUpdatedAt" : 1455876727, "rowsUpdatedBy" : "ajn4-zy65", "state" : "normal", "tableId" : 1722160, "totalTimesRated" : 0, "viewCount" : 6910, "viewLastModified" : 1434558076, "viewType" : "tabular", "columns" : [ { "disabledFeatureFlags" : [ "allow_comments" ], "grants" : [ { "metadata" : { "owner" : { "query" : { "rights" : [ "read" ], "sortBys" : [ { "tableAuthor" : { "tags" : [ "traffic", "stop", "violations", "electronic issued." ], "flags" : [ "default" ]"data" : [ [ 1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050", 1455876689, "498050", null, "2016-02-18T00:00:00", "09:05:00", "MCP", "2nd district, Bethesda", "DRIVER USING HANDS TO USE HANDHELD TELEPHONE WHILEMOTOR VEHICLE IS IN MOTION", "355/TUCKERMAN LN", "-77.105925", "39.03223", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "MD", "02 - Automobile", "2010", "JEEP", "CRISWELL", "BLUE", "Citation", "21-1124.2[d2]", "Transportation Article", "No", "WHITE", "F", "GERMANTOWN", "MD", "MD", "A - Marked Patrol", [ "{\"address\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\"}", "-77.105925", "39.03223", null, false ] ]
9. Sau đó, chúng tôi chỉ định
import ijson
filename = "md_traffic.json"
with open[filename, 'r'] as f:
    objects = ijson.items[f, 'meta.view.columns.item']
    columns = list[objects]
7 để chỉ ra rằng chúng tôi nên trích xuất từng mục riêng lẻ trong danh sách
import ijson
filename = "md_traffic.json"
with open[filename, 'r'] as f:
    objects = ijson.items[f, 'meta.view.columns.item']
    columns = list[objects]
3. Hàm
import ijson
filename = "md_traffic.json"
with open[filename, 'r'] as f:
    objects = ijson.items[f, 'meta.view.columns.item']
    columns = list[objects]
2 sẽ trả về một trình tạo, vì vậy chúng tôi sử dụng phương thức list để biến trình tạo thành danh sách Python. Chúng tôi có thể in ra mục đầu tiên trong danh sách

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
0
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
1

Từ kết quả trên, có vẻ như mỗi mục trong

"meta" : { "view" : { "id" : "4mse-ku6q", "name" : "Traffic Violations", "averageRating" : 0, "category" : "Public Safety", "createdAt" : 1403103517, "description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily", "displayType" : "table", "downloadCount" : 2851, "iconUrl" : "fileId:r41tDc239M1FL75LFwXFKzFCWqr8mzMeMTYXiA24USM", "indexUpdatedAt" : 1455885508, "newBackend" : false, "numberOfComments" : 0, "oid" : 8890705, "publicationAppendEnabled" : false, "publicationDate" : 1411040702, "publicationGroup" : 1620779, "publicationStage" : "published", "rowClass" : "", "rowsUpdatedAt" : 1455876727, "rowsUpdatedBy" : "ajn4-zy65", "state" : "normal", "tableId" : 1722160, "totalTimesRated" : 0, "viewCount" : 6910, "viewLastModified" : 1434558076, "viewType" : "tabular", "columns" : [ { "disabledFeatureFlags" : [ "allow_comments" ], "grants" : [ { "metadata" : { "owner" : { "query" : { "rights" : [ "read" ], "sortBys" : [ { "tableAuthor" : { "tags" : [ "traffic", "stop", "violations", "electronic issued." ], "flags" : [ "default" ]"data" : [ [ 1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050", 1455876689, "498050", null, "2016-02-18T00:00:00", "09:05:00", "MCP", "2nd district, Bethesda", "DRIVER USING HANDS TO USE HANDHELD TELEPHONE WHILEMOTOR VEHICLE IS IN MOTION", "355/TUCKERMAN LN", "-77.105925", "39.03223", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "MD", "02 - Automobile", "2010", "JEEP", "CRISWELL", "BLUE", "Citation", "21-1124.2[d2]", "Transportation Article", "No", "WHITE", "F", "GERMANTOWN", "MD", "MD", "A - Marked Patrol", [ "{\"address\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\"}", "-77.105925", "39.03223", null, false ] ]
9 là một từ điển chứa thông tin về từng cột. Để có được tiêu đề của chúng tôi, có vẻ như
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
01 là khóa có liên quan để trích xuất. Để lấy tên cột của chúng tôi, chúng tôi chỉ cần trích xuất khóa
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
01 từ mỗi mục trong
"meta" : { "view" : { "id" : "4mse-ku6q", "name" : "Traffic Violations", "averageRating" : 0, "category" : "Public Safety", "createdAt" : 1403103517, "description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily", "displayType" : "table", "downloadCount" : 2851, "iconUrl" : "fileId:r41tDc239M1FL75LFwXFKzFCWqr8mzMeMTYXiA24USM", "indexUpdatedAt" : 1455885508, "newBackend" : false, "numberOfComments" : 0, "oid" : 8890705, "publicationAppendEnabled" : false, "publicationDate" : 1411040702, "publicationGroup" : 1620779, "publicationStage" : "published", "rowClass" : "", "rowsUpdatedAt" : 1455876727, "rowsUpdatedBy" : "ajn4-zy65", "state" : "normal", "tableId" : 1722160, "totalTimesRated" : 0, "viewCount" : 6910, "viewLastModified" : 1434558076, "viewType" : "tabular", "columns" : [ { "disabledFeatureFlags" : [ "allow_comments" ], "grants" : [ { "metadata" : { "owner" : { "query" : { "rights" : [ "read" ], "sortBys" : [ { "tableAuthor" : { "tags" : [ "traffic", "stop", "violations", "electronic issued." ], "flags" : [ "default" ]"data" : [ [ 1889194, "92AD0076-5308-45D0-BDE3-6A3A55AD9A04", 1889194, 1455876689, "498050", 1455876689, "498050", null, "2016-02-18T00:00:00", "09:05:00", "MCP", "2nd district, Bethesda", "DRIVER USING HANDS TO USE HANDHELD TELEPHONE WHILEMOTOR VEHICLE IS IN MOTION", "355/TUCKERMAN LN", "-77.105925", "39.03223", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "MD", "02 - Automobile", "2010", "JEEP", "CRISWELL", "BLUE", "Citation", "21-1124.2[d2]", "Transportation Article", "No", "WHITE", "F", "GERMANTOWN", "MD", "MD", "A - Marked Patrol", [ "{\"address\":\"\",\"city\":\"\",\"state\":\"\",\"zip\":\"\"}", "-77.105925", "39.03223", null, false ] ]
9

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
2_______3_______3

Tuyệt vời. Bây giờ chúng ta có tên cột, chúng ta có thể chuyển sang trích xuất dữ liệu

Trích xuất dữ liệu

Bạn có thể nhớ rằng dữ liệu bị khóa trong một danh sách các danh sách bên trong khóa

%%bashgrep -E '^ {2,6}"' md_traffic.json
3. Chúng tôi sẽ cần đọc dữ liệu này vào bộ nhớ để thao tác với nó. May mắn thay, chúng ta có thể sử dụng các tên cột mà chúng ta vừa trích xuất để chỉ lấy các cột có liên quan. Điều này sẽ tiết kiệm rất nhiều không gian. Nếu tập dữ liệu lớn hơn, bạn có thể xử lý lặp lại các lô hàng. Vì vậy, hãy đọc trong các hàng
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
05 đầu tiên, thực hiện một số xử lý, sau đó là
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
05 tiếp theo, v.v. Trong trường hợp này, chúng tôi có thể xác định các cột mà chúng tôi quan tâm và sử dụng lại
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
07 để xử lý lặp lại tệp JSON

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
4

Bây giờ chúng ta đã đọc dữ liệu vào, chúng ta có thể in ra mục đầu tiên trong

%%bashgrep -E '^ {2,6}"' md_traffic.json
3

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
5
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
6

Đọc dữ liệu vào Pandas

Bây giờ chúng tôi có dữ liệu dưới dạng danh sách các danh sách và tiêu đề cột dưới dạng danh sách, chúng tôi có thể tạo Khung dữ liệu Pandas để phân tích dữ liệu. Nếu bạn không quen thuộc với Pandas, thì đó là một thư viện phân tích dữ liệu sử dụng cấu trúc dữ liệu dạng bảng, hiệu quả được gọi là Dataframe để thể hiện dữ liệu của bạn. Pandas cho phép bạn chuyển đổi danh sách các danh sách thành Dataframe và chỉ định riêng tên cột

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
7

Bây giờ chúng tôi có dữ liệu của mình trong Dataframe, chúng tôi có thể thực hiện một số phân tích thú vị. Đây là bảng có bao nhiêu điểm dừng được thực hiện theo màu xe

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
8
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
9

Ngụy trang dường như là một màu xe rất phổ biến. Đây là bảng về loại đơn vị cảnh sát nào đã tạo ra trích dẫn

%%bashhead md_traffic.json
0
%%bashhead md_traffic.json
1

Với sự gia tăng của camera vượt đèn đỏ và laser bắn tốc độ, điều thú vị là xe tuần tra cho đến nay vẫn là nguồn trích dẫn chủ đạo

Chuyển đổi cột

Bây giờ chúng tôi gần như đã sẵn sàng để thực hiện một số phân tích dựa trên thời gian và vị trí, nhưng trước tiên chúng tôi cần chuyển đổi các cột

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
09,
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
10 và
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
11 từ chuỗi thành số float. Chúng tôi có thể sử dụng đoạn mã dưới đây để chuyển đổi
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
10 và
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
09

%%bashhead md_traffic.json
2

Thật kỳ lạ, thời gian trong ngày và ngày dừng lại được lưu trữ trong hai cột riêng biệt,

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
14 và
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
15. Chúng tôi sẽ phân tích cú pháp cả hai và biến chúng thành một cột ngày giờ duy nhất

%%bashhead md_traffic.json
3

Giờ đây, chúng tôi có thể lập biểu đồ về những ngày có nhiều điểm dừng lưu lượng truy cập nhất

%%bashhead md_traffic.json
4_______5_______5

Trong cốt truyện này, Thứ Hai là

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
16 và Chủ nhật là
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
17. Có vẻ như Chủ Nhật có nhiều điểm dừng nhất và Thứ Hai có ít điểm dừng nhất. Đây cũng có thể là vấn đề về chất lượng dữ liệu khi ngày không hợp lệ dẫn đến Chủ nhật vì một số lý do. Bạn sẽ phải đào sâu hơn vào cột
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
15 để tìm ra nó một cách dứt khoát [điều này nằm ngoài phạm vi của bài đăng này]

Chúng tôi cũng có thể vạch ra thời gian dừng giao thông phổ biến nhất

%%bashhead md_traffic.json
6_______5_______7

Có vẻ như nhiều điểm dừng nhất xảy ra vào khoảng nửa đêm và ít điểm dừng nhất xảy ra vào khoảng 5 giờ sáng. Điều này có thể hợp lý, vì mọi người đang lái xe về nhà từ quán bar và bữa tối muộn và đêm, và có thể bị suy giảm. Đây cũng có thể là vấn đề về chất lượng dữ liệu và cần xem qua cột

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
14 để có câu trả lời đầy đủ

Đặt lại các điểm dừng

Bây giờ chúng tôi đã chuyển đổi các cột vị trí và ngày, chúng tôi có thể vạch ra các điểm dừng giao thông. Vì ánh xạ rất tốn tài nguyên CPU và bộ nhớ, trước tiên, chúng tôi cần lọc xuống các hàng mà chúng tôi sử dụng từ

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
20

%%bashhead md_traffic.json
8

Trong đoạn mã trên, chúng tôi đã chọn tất cả các hàng xuất hiện trong năm qua. Chúng tôi có thể thu hẹp phạm vi này hơn nữa và chỉ chọn các hàng xảy ra trong giờ cao điểm — khoảng thời gian buổi sáng khi mọi người đi làm

%%bashhead md_traffic.json
9
{ "meta" : {
"view" : {
"id" : "4mse-ku6q",
"name" : "Traffic Violations",
"averageRating" : 0,
"category" : "Public Safety",
"createdAt" : 1403103517,
"description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily",
"displayType" : "table",
0
{ "meta" : {
"view" : {
"id" : "4mse-ku6q",
"name" : "Traffic Violations",
"averageRating" : 0,
"category" : "Public Safety",
"createdAt" : 1403103517,
"description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily",
"displayType" : "table",
1

Sử dụng gói folium tuyệt vời, giờ đây chúng ta có thể hình dung nơi tất cả các điểm dừng xảy ra. Folium cho phép bạn dễ dàng tạo bản đồ tương tác bằng Python bằng cách tận dụng tờ rơi. Để duy trì hiệu suất, chúng tôi sẽ chỉ hình dung

{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
21 hàng đầu tiên của
{'event_type': 'started-lesson', 'keen': {'created_at': '2015-06-12T23:09:03.966Z', 'id': '557b668fd2eaaa2e7c5e916b', 'timestamp': '2015-06-12T23:09:07.971Z'}, 'sequence': 1} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:03.979Z', 'id': '557b668f90e4bd26c10b6ed6', 'timestamp': '2015-06-12T23:09:07.987Z'}, 'lesson': 1, 'sequence': 4, 'type': 'code'} {'event_type': 'started-screen', 'keen': {'created_at': '2015-06-12T23:09:22.517Z', 'id': '557b66a246f9a7239038b1e0', 'timestamp': '2015-06-12T23:09:24.246Z'}, 'lesson': 1, 'sequence': 3, 'type': 'code'},
22

{ "meta" : {
"view" : {
"id" : "4mse-ku6q",
"name" : "Traffic Violations",
"averageRating" : 0,
"category" : "Public Safety",
"createdAt" : 1403103517,
"description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily",
"displayType" : "table",
2

Điều này cho thấy nhiều điểm dừng giao thông tập trung quanh phía dưới bên phải của quận. Chúng tôi có thể mở rộng phân tích của mình hơn nữa bằng bản đồ nhiệt

{ "meta" : {
"view" : {
"id" : "4mse-ku6q",
"name" : "Traffic Violations",
"averageRating" : 0,
"category" : "Public Safety",
"createdAt" : 1403103517,
"description" : "This dataset contains traffic violation information from all electronic traffic violations issued in the County. Any information that can be used to uniquely identify the vehicle, the vehicle owner or the officer issuing the violation will not be published.\r\n\r\nUpdate Frequency: Daily",
"displayType" : "table",
3

Bước tiếp theo

Trong bài đăng này, tập trung vào việc học lập trình python, chúng ta đã học cách sử dụng Python để chuyển từ dữ liệu JSON thô sang bản đồ đầy đủ chức năng bằng các công cụ dòng lệnh, ijson, Pandas, matplotlib và folium. Nếu bạn muốn tìm hiểu thêm về các công cụ này, hãy xem các khóa học Phân tích dữ liệu, Trực quan hóa dữ liệu và Dòng lệnh của chúng tôi trên Dataquest

Nếu bạn muốn khám phá thêm tập dữ liệu này, đây là một số câu hỏi thú vị để trả lời

  • Loại điểm dừng có thay đổi theo vị trí không?
  • Làm thế nào để thu nhập tương quan với số điểm dừng?
  • Làm thế nào để mật độ dân số tương quan với số điểm dừng?
  • Những loại điểm dừng phổ biến nhất vào khoảng nửa đêm?

Bước tiếp theo

Trong bài đăng này, tập trung vào việc học lập trình python, chúng ta đã học cách sử dụng Python để chuyển từ dữ liệu JSON thô sang bản đồ đầy đủ chức năng bằng các công cụ dòng lệnh, ijson, Pandas, matplotlib và folium. Nếu bạn muốn tìm hiểu thêm về các công cụ này, hãy xem các khóa học Phân tích dữ liệu, Trực quan hóa dữ liệu và Dòng lệnh của chúng tôi trên Dataquest

Nếu bạn muốn khám phá thêm tập dữ liệu này, đây là một số câu hỏi thú vị để trả lời

  • Loại điểm dừng có thay đổi theo vị trí không?
  • Làm thế nào để thu nhập tương quan với số điểm dừng?
  • Làm thế nào để mật độ dân số tương quan với số điểm dừng?
  • Những loại điểm dừng phổ biến nhất vào khoảng nửa đêm?

Hướng dẫn này có hữu ích không?

Chọn con đường của bạn để tiếp tục học các kỹ năng dữ liệu có giá trị

Hướng dẫn Python

Thực hành các kỹ năng lập trình Python của bạn khi bạn làm việc với các hướng dẫn miễn phí của chúng tôi

Các khóa học khoa học dữ liệu

Cam kết học tập với các khóa học khoa học dữ liệu tương tác, trong trình duyệt của bạn bằng Python, R, SQL, v.v.

crimeData ScienceintermediatejsonLearn PythonMatplotlibPandaspythonSQLtraffictraffic violationstutorialTutorials

Làm cách nào để tải tệp JSON khổng lồ Python?

Bạn có thể tải tệp json vào chương trình Python bằng cách sử dụng json. load[] - điều này tạo ra một đối tượng Python mà sau đó có thể được thao tác dễ dàng giống như bất kỳ danh sách Python, từ điển hoặc giá trị nào khác. Sau đó, bạn có thể viết một đối tượng Python dưới dạng JSON bằng cách sử dụng json.

Python xử lý các tệp JSON lớn như thế nào?

Như thường lệ, bạn có thể thử các giải pháp khác. .
gấu trúc. Pandas có khả năng đọc JSON và về lý thuyết, nó có thể làm điều đó theo cách tiết kiệm bộ nhớ hơn cho một số bố cục JSON nhất định. .
SQLite. Cơ sở dữ liệu SQLite có thể phân tích cú pháp JSON, lưu trữ JSON trong các cột và truy vấn JSON [xem tài liệu]

Làm cách nào để đọc tệp JSON theo khối?

Thay vì đọc toàn bộ tệp cùng một lúc, tham số 'chunksize' sẽ tạo ra một trình đọc có số lượng dòng cụ thể được đọc mỗi lần và tùy theo độ dài tệp của bạn, một lượng khối nhất định sẽ được tạo và đẩy . 000 dòng và bạn

Kích thước bao nhiêu là quá lớn đối với một tệp JSON?

Tài liệu JSON có thể lớn đến mức nào? . Câu trả lời ngắn gọn là kích thước tối đa là 1GB . Tuy nhiên, JSON thường thay đổi cách thực hiện mô hình hóa dữ liệu và xứng đáng nhận được phản hồi dài hơn một chút.

Chủ Đề