{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"import os\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# a = pd.read_csv('./DOT_Traffic_Speeds_NBE.csv')\n",
"# save it as parquet\n",
"# a.to_parquet('./DOT_Traffic_Speeds_NBE.parquet')\n",
"a = pd.read_parquet('./DOT_Traffic_Speeds_NBE.parquet')\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"a = a.drop(columns=['ENCODED_POLY_LINE', 'ENCODED_POLY_LINE_LVLS', 'OWNER', 'TRANSCOM_ID', 'LINK_ID'])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.microsoft.datawrangler.viewer.v0+json": {
"columns": [
{
"name": "index",
"rawType": "int64",
"type": "integer"
},
{
"name": "ID",
"rawType": "int64",
"type": "integer"
},
{
"name": "SPEED",
"rawType": "float64",
"type": "float"
},
{
"name": "TRAVEL_TIME",
"rawType": "int64",
"type": "integer"
},
{
"name": "STATUS",
"rawType": "int64",
"type": "integer"
},
{
"name": "DATA_AS_OF",
"rawType": "object",
"type": "string"
},
{
"name": "LINK_POINTS",
"rawType": "object",
"type": "string"
},
{
"name": "BOROUGH",
"rawType": "object",
"type": "string"
},
{
"name": "LINK_NAME",
"rawType": "object",
"type": "string"
}
],
"conversionMethod": "pd.DataFrame",
"ref": "4c9fbe26-9050-4954-9933-50a4589a7667",
"rows": [
[
"0",
"262",
"34.8",
"359",
"0",
"06/02/2017 11:41:59 PM",
"40.6332305,-74.016151 40.63391,-74.01613 40.6343505,-74.016241 40.63485,-74.016501 40.63533,-74.01694 40.63622,-74.01827 40.6375704,-74.02046 40.63803,-74.021071 40.6385,-74.02146 40.63908,-74.02173 40.63967,-74.02185 40.64036,-74.02172 40.64103,-74.02143",
"Brooklyn",
"GOW S 9TH STREET - 7TH AVENUE"
],
[
"1",
"204",
"55.92",
"155",
"0",
"06/02/2017 11:41:59 PM",
"40.7894406,-73.786291 40.78918,-73.78792 40.78876,-73.789521 40.7882704,-73.790631 40.7872906,-73.79263 40.7866804,-73.7941 40.7864,-73.794901 40.7861204,-73.796241 40.78602,-73.79693 40.78596,-73.79837 40.78615,-73.80182 40.786331,-73.80525 4",
"Queens",
"CIP N TNB - Whitestone Expwy S Exit 14 (Linden Pl)"
],
[
"2",
"106",
"39.77",
"159",
"0",
"06/02/2017 11:41:59 PM",
"40.77158,-73.994441 40.7713004,-73.99455 40.77085,-73.99467 40.76997,-73.99481 40.7701604,-73.99477 40.76986,-73.994831 40.7695406,-73.99496 40.769341,-73.99508 40.768311,-73.9958 40.768311,-73.9958 40.76623,-73.99733 40.76623,-73.99733 40.76547,-73.9979",
"Manhattan",
"12th Ave S 57th St - 45th St"
],
[
"3",
"184",
"65.24",
"39",
"0",
"06/03/2017 04:46:59 AM",
"40.8347204,-73.86593 40.83357,-73.86199 40.8327305,-73.8592 40.8312,-73.85409 40.83017,-73.85069 40.83015,-73.85071 40.8301304,-73.850731 40.8301105,-73.850731",
"Bronx",
"CBE E TAYLOR AVENUE - CASTLE HILL AVENUE"
],
[
"4",
"3",
"14.91",
"422",
"0",
"06/02/2017 11:41:59 PM",
"40.76375,-73.999191 40.763521,-73.99935 40.7620804,-74.00136 40.75985,-74.00306 40.75775,-74.00457 40.75775,-74.00457 40.75576,-74.00601 40.7544904,-74.006921 40.7538404,-74.007241 40.75415,-74.00712 40.7502804,-74.00848 40.74833,-74.007771 40.74114,-74.0",
"Manhattan",
"12th ave @ 45th - 11 ave ganservoort st"
]
],
"shape": {
"columns": 8,
"rows": 5
}
},
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" ID | \n",
" SPEED | \n",
" TRAVEL_TIME | \n",
" STATUS | \n",
" DATA_AS_OF | \n",
" LINK_POINTS | \n",
" BOROUGH | \n",
" LINK_NAME | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 262 | \n",
" 34.80 | \n",
" 359 | \n",
" 0 | \n",
" 06/02/2017 11:41:59 PM | \n",
" 40.6332305,-74.016151 40.63391,-74.01613 40.63... | \n",
" Brooklyn | \n",
" GOW S 9TH STREET - 7TH AVENUE | \n",
"
\n",
" \n",
" | 1 | \n",
" 204 | \n",
" 55.92 | \n",
" 155 | \n",
" 0 | \n",
" 06/02/2017 11:41:59 PM | \n",
" 40.7894406,-73.786291 40.78918,-73.78792 40.... | \n",
" Queens | \n",
" CIP N TNB - Whitestone Expwy S Exit 14 (Linden... | \n",
"
\n",
" \n",
" | 2 | \n",
" 106 | \n",
" 39.77 | \n",
" 159 | \n",
" 0 | \n",
" 06/02/2017 11:41:59 PM | \n",
" 40.77158,-73.994441 40.7713004,-73.99455 40.77... | \n",
" Manhattan | \n",
" 12th Ave S 57th St - 45th St | \n",
"
\n",
" \n",
" | 3 | \n",
" 184 | \n",
" 65.24 | \n",
" 39 | \n",
" 0 | \n",
" 06/03/2017 04:46:59 AM | \n",
" 40.8347204,-73.86593 40.83357,-73.86199 40.832... | \n",
" Bronx | \n",
" CBE E TAYLOR AVENUE - CASTLE HILL AVENUE | \n",
"
\n",
" \n",
" | 4 | \n",
" 3 | \n",
" 14.91 | \n",
" 422 | \n",
" 0 | \n",
" 06/02/2017 11:41:59 PM | \n",
" 40.76375,-73.999191 40.763521,-73.99935 40.762... | \n",
" Manhattan | \n",
" 12th ave @ 45th - 11 ave ganservoort st | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" ID SPEED TRAVEL_TIME STATUS DATA_AS_OF \\\n",
"0 262 34.80 359 0 06/02/2017 11:41:59 PM \n",
"1 204 55.92 155 0 06/02/2017 11:41:59 PM \n",
"2 106 39.77 159 0 06/02/2017 11:41:59 PM \n",
"3 184 65.24 39 0 06/03/2017 04:46:59 AM \n",
"4 3 14.91 422 0 06/02/2017 11:41:59 PM \n",
"\n",
" LINK_POINTS BOROUGH \\\n",
"0 40.6332305,-74.016151 40.63391,-74.01613 40.63... Brooklyn \n",
"1 40.7894406,-73.786291 40.78918,-73.78792 40.... Queens \n",
"2 40.77158,-73.994441 40.7713004,-73.99455 40.77... Manhattan \n",
"3 40.8347204,-73.86593 40.83357,-73.86199 40.832... Bronx \n",
"4 40.76375,-73.999191 40.763521,-73.99935 40.762... Manhattan \n",
"\n",
" LINK_NAME \n",
"0 GOW S 9TH STREET - 7TH AVENUE \n",
"1 CIP N TNB - Whitestone Expwy S Exit 14 (Linden... \n",
"2 12th Ave S 57th St - 45th St \n",
"3 CBE E TAYLOR AVENUE - CASTLE HILL AVENUE \n",
"4 12th ave @ 45th - 11 ave ganservoort st "
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.head()\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"a['DATA_AS_OF'] = pd.to_datetime(a['DATA_AS_OF'], format='%m/%d/%Y %I:%M:%S %p').dt.strftime('%Y%m%d%H%M%S')"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.microsoft.datawrangler.viewer.v0+json": {
"columns": [
{
"name": "index",
"rawType": "int64",
"type": "integer"
},
{
"name": "ID",
"rawType": "int64",
"type": "integer"
},
{
"name": "SPEED",
"rawType": "float64",
"type": "float"
},
{
"name": "TRAVEL_TIME",
"rawType": "int64",
"type": "integer"
},
{
"name": "STATUS",
"rawType": "int64",
"type": "integer"
},
{
"name": "DATA_AS_OF",
"rawType": "object",
"type": "string"
},
{
"name": "LINK_POINTS",
"rawType": "object",
"type": "string"
},
{
"name": "BOROUGH",
"rawType": "object",
"type": "string"
},
{
"name": "LINK_NAME",
"rawType": "object",
"type": "string"
}
],
"conversionMethod": "pd.DataFrame",
"ref": "1d545d60-5766-4a44-9873-2b42cfb56dfc",
"rows": [
[
"0",
"262",
"34.8",
"359",
"0",
"20170602234159",
"40.6332305,-74.016151 40.63391,-74.01613 40.6343505,-74.016241 40.63485,-74.016501 40.63533,-74.01694 40.63622,-74.01827 40.6375704,-74.02046 40.63803,-74.021071 40.6385,-74.02146 40.63908,-74.02173 40.63967,-74.02185 40.64036,-74.02172 40.64103,-74.02143",
"Brooklyn",
"GOW S 9TH STREET - 7TH AVENUE"
],
[
"1",
"204",
"55.92",
"155",
"0",
"20170602234159",
"40.7894406,-73.786291 40.78918,-73.78792 40.78876,-73.789521 40.7882704,-73.790631 40.7872906,-73.79263 40.7866804,-73.7941 40.7864,-73.794901 40.7861204,-73.796241 40.78602,-73.79693 40.78596,-73.79837 40.78615,-73.80182 40.786331,-73.80525 4",
"Queens",
"CIP N TNB - Whitestone Expwy S Exit 14 (Linden Pl)"
],
[
"2",
"106",
"39.77",
"159",
"0",
"20170602234159",
"40.77158,-73.994441 40.7713004,-73.99455 40.77085,-73.99467 40.76997,-73.99481 40.7701604,-73.99477 40.76986,-73.994831 40.7695406,-73.99496 40.769341,-73.99508 40.768311,-73.9958 40.768311,-73.9958 40.76623,-73.99733 40.76623,-73.99733 40.76547,-73.9979",
"Manhattan",
"12th Ave S 57th St - 45th St"
],
[
"3",
"184",
"65.24",
"39",
"0",
"20170603044659",
"40.8347204,-73.86593 40.83357,-73.86199 40.8327305,-73.8592 40.8312,-73.85409 40.83017,-73.85069 40.83015,-73.85071 40.8301304,-73.850731 40.8301105,-73.850731",
"Bronx",
"CBE E TAYLOR AVENUE - CASTLE HILL AVENUE"
],
[
"4",
"3",
"14.91",
"422",
"0",
"20170602234159",
"40.76375,-73.999191 40.763521,-73.99935 40.7620804,-74.00136 40.75985,-74.00306 40.75775,-74.00457 40.75775,-74.00457 40.75576,-74.00601 40.7544904,-74.006921 40.7538404,-74.007241 40.75415,-74.00712 40.7502804,-74.00848 40.74833,-74.007771 40.74114,-74.0",
"Manhattan",
"12th ave @ 45th - 11 ave ganservoort st"
]
],
"shape": {
"columns": 8,
"rows": 5
}
},
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" ID | \n",
" SPEED | \n",
" TRAVEL_TIME | \n",
" STATUS | \n",
" DATA_AS_OF | \n",
" LINK_POINTS | \n",
" BOROUGH | \n",
" LINK_NAME | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 262 | \n",
" 34.80 | \n",
" 359 | \n",
" 0 | \n",
" 20170602234159 | \n",
" 40.6332305,-74.016151 40.63391,-74.01613 40.63... | \n",
" Brooklyn | \n",
" GOW S 9TH STREET - 7TH AVENUE | \n",
"
\n",
" \n",
" | 1 | \n",
" 204 | \n",
" 55.92 | \n",
" 155 | \n",
" 0 | \n",
" 20170602234159 | \n",
" 40.7894406,-73.786291 40.78918,-73.78792 40.... | \n",
" Queens | \n",
" CIP N TNB - Whitestone Expwy S Exit 14 (Linden... | \n",
"
\n",
" \n",
" | 2 | \n",
" 106 | \n",
" 39.77 | \n",
" 159 | \n",
" 0 | \n",
" 20170602234159 | \n",
" 40.77158,-73.994441 40.7713004,-73.99455 40.77... | \n",
" Manhattan | \n",
" 12th Ave S 57th St - 45th St | \n",
"
\n",
" \n",
" | 3 | \n",
" 184 | \n",
" 65.24 | \n",
" 39 | \n",
" 0 | \n",
" 20170603044659 | \n",
" 40.8347204,-73.86593 40.83357,-73.86199 40.832... | \n",
" Bronx | \n",
" CBE E TAYLOR AVENUE - CASTLE HILL AVENUE | \n",
"
\n",
" \n",
" | 4 | \n",
" 3 | \n",
" 14.91 | \n",
" 422 | \n",
" 0 | \n",
" 20170602234159 | \n",
" 40.76375,-73.999191 40.763521,-73.99935 40.762... | \n",
" Manhattan | \n",
" 12th ave @ 45th - 11 ave ganservoort st | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" ID SPEED TRAVEL_TIME STATUS DATA_AS_OF \\\n",
"0 262 34.80 359 0 20170602234159 \n",
"1 204 55.92 155 0 20170602234159 \n",
"2 106 39.77 159 0 20170602234159 \n",
"3 184 65.24 39 0 20170603044659 \n",
"4 3 14.91 422 0 20170602234159 \n",
"\n",
" LINK_POINTS BOROUGH \\\n",
"0 40.6332305,-74.016151 40.63391,-74.01613 40.63... Brooklyn \n",
"1 40.7894406,-73.786291 40.78918,-73.78792 40.... Queens \n",
"2 40.77158,-73.994441 40.7713004,-73.99455 40.77... Manhattan \n",
"3 40.8347204,-73.86593 40.83357,-73.86199 40.832... Bronx \n",
"4 40.76375,-73.999191 40.763521,-73.99935 40.762... Manhattan \n",
"\n",
" LINK_NAME \n",
"0 GOW S 9TH STREET - 7TH AVENUE \n",
"1 CIP N TNB - Whitestone Expwy S Exit 14 (Linden... \n",
"2 12th Ave S 57th St - 45th St \n",
"3 CBE E TAYLOR AVENUE - CASTLE HILL AVENUE \n",
"4 12th ave @ 45th - 11 ave ganservoort st "
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---------------------------------"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[262,\n",
" 204,\n",
" 106,\n",
" 184,\n",
" 3,\n",
" 2,\n",
" 217,\n",
" 221,\n",
" 1,\n",
" 4,\n",
" 149,\n",
" 150,\n",
" 223,\n",
" 145,\n",
" 450,\n",
" 208,\n",
" 199,\n",
" 207,\n",
" 331,\n",
" 332,\n",
" 169,\n",
" 170,\n",
" 171,\n",
" 315,\n",
" 325,\n",
" 440,\n",
" 330,\n",
" 329,\n",
" 195,\n",
" 345,\n",
" 344,\n",
" 191,\n",
" 213,\n",
" 399,\n",
" 395,\n",
" 410,\n",
" 347,\n",
" 141,\n",
" 140,\n",
" 398,\n",
" 202,\n",
" 168,\n",
" 164,\n",
" 298,\n",
" 451,\n",
" 406,\n",
" 364,\n",
" 417,\n",
" 416,\n",
" 411,\n",
" 264,\n",
" 263,\n",
" 190,\n",
" 448,\n",
" 212,\n",
" 318,\n",
" 211,\n",
" 319,\n",
" 311,\n",
" 419,\n",
" 418,\n",
" 387,\n",
" 385,\n",
" 390,\n",
" 351,\n",
" 388,\n",
" 434,\n",
" 430,\n",
" 431,\n",
" 433,\n",
" 412,\n",
" 413,\n",
" 258,\n",
" 259,\n",
" 394,\n",
" 261,\n",
" 154,\n",
" 155,\n",
" 153,\n",
" 453,\n",
" 428,\n",
" 129,\n",
" 126,\n",
" 295,\n",
" 159,\n",
" 338,\n",
" 427,\n",
" 142,\n",
" 160,\n",
" 185,\n",
" 426,\n",
" 425,\n",
" 110,\n",
" 369,\n",
" 178,\n",
" 422,\n",
" 423,\n",
" 424,\n",
" 165,\n",
" 177,\n",
" 172,\n",
" 167,\n",
" 257,\n",
" 222,\n",
" 379,\n",
" 324,\n",
" 215,\n",
" 383,\n",
" 186,\n",
" 402,\n",
" 265,\n",
" 380,\n",
" 445,\n",
" 119,\n",
" 137,\n",
" 124,\n",
" 365,\n",
" 354,\n",
" 349,\n",
" 205,\n",
" 439,\n",
" 206,\n",
" 405,\n",
" 376,\n",
" 375,\n",
" 381,\n",
" 377,\n",
" 350,\n",
" 378,\n",
" 435,\n",
" 384,\n",
" 441,\n",
" 436,\n",
" 437,\n",
" 382,\n",
" 316,\n",
" 148,\n",
" 157,\n",
" 339,\n",
" 432,\n",
" 389,\n",
" 108,\n",
" 107,\n",
" 225,\n",
" 446,\n",
" 444,\n",
" 224,\n",
" 442,\n",
" 447,\n",
" 443,\n",
" 122,\n",
" 123,\n",
" 313]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# get the unique values of ID\n",
"id_list = a['ID'].unique().tolist()\n",
"id_list"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.microsoft.datawrangler.viewer.v0+json": {
"columns": [
{
"name": "index",
"rawType": "int64",
"type": "integer"
},
{
"name": "ID",
"rawType": "int64",
"type": "integer"
},
{
"name": "SPEED",
"rawType": "float64",
"type": "float"
},
{
"name": "TRAVEL_TIME",
"rawType": "int64",
"type": "integer"
},
{
"name": "STATUS",
"rawType": "int64",
"type": "integer"
},
{
"name": "DATA_AS_OF",
"rawType": "object",
"type": "string"
},
{
"name": "LINK_POINTS",
"rawType": "object",
"type": "string"
},
{
"name": "BOROUGH",
"rawType": "object",
"type": "string"
},
{
"name": "LINK_NAME",
"rawType": "object",
"type": "string"
}
],
"conversionMethod": "pd.DataFrame",
"ref": "88ca17ea-1389-41fa-ba6c-2c211baa054a",
"rows": [
[
"0",
"444",
"0.0",
"0",
"-101",
"19780101000000",
"40.7051105,-74.01663 40.70432,-74.01667 40.7035205,-74.016561 40.70224,-74.015841 40.7016205,-74.01527 40.701401,-74.01473 40.7012804,-74.01399 40.7012,-74.01327 40.701401,-74.012031 40.7018906,-74.00995 40.70306,-74.007731 40.70481,-74.004951 40.70656,-7",
"Manhattan",
"West St S Battery Pl - FDR N Catherine Slip"
],
[
"1",
"444",
"13.05",
"105",
"0",
"20151208152435",
"40.7051105,-74.01663 40.70432,-74.01667 40.7035205,-74.016561 40.70224,-74.015841 40.7016205,-74.01527 40.701401,-74.01473 40.7012804,-74.01399 40.7012,-74.01327 40.701401,-74.012031 40.7018906,-74.00995 40.70306,-74.007731 40.70481,-74.004951 40.70656,-7",
"Manhattan",
"West St S Battery Pl - FDR N Catherine Slip"
]
],
"shape": {
"columns": 8,
"rows": 2
}
},
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" ID | \n",
" SPEED | \n",
" TRAVEL_TIME | \n",
" STATUS | \n",
" DATA_AS_OF | \n",
" LINK_POINTS | \n",
" BOROUGH | \n",
" LINK_NAME | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 444 | \n",
" 0.00 | \n",
" 0 | \n",
" -101 | \n",
" 19780101000000 | \n",
" 40.7051105,-74.01663 40.70432,-74.01667 40.703... | \n",
" Manhattan | \n",
" West St S Battery Pl - FDR N Catherine Slip | \n",
"
\n",
" \n",
" | 1 | \n",
" 444 | \n",
" 13.05 | \n",
" 105 | \n",
" 0 | \n",
" 20151208152435 | \n",
" 40.7051105,-74.01663 40.70432,-74.01667 40.703... | \n",
" Manhattan | \n",
" West St S Battery Pl - FDR N Catherine Slip | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" ID SPEED TRAVEL_TIME STATUS DATA_AS_OF \\\n",
"0 444 0.00 0 -101 19780101000000 \n",
"1 444 13.05 105 0 20151208152435 \n",
"\n",
" LINK_POINTS BOROUGH \\\n",
"0 40.7051105,-74.01663 40.70432,-74.01667 40.703... Manhattan \n",
"1 40.7051105,-74.01663 40.70432,-74.01667 40.703... Manhattan \n",
"\n",
" LINK_NAME \n",
"0 West St S Battery Pl - FDR N Catherine Slip \n",
"1 West St S Battery Pl - FDR N Catherine Slip "
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = a[a['ID'].apply(str).str.contains(\"444\", regex=False, na=False, case=False)]\n",
"# convert DATA_AS_OF from %m/%d/%Y %I:%M:%S %p to %Y%m%d%H%M%S\n",
"# df['DATA_AS_OF'] = pd.to_datetime(df['DATA_AS_OF'], format='%m/%d/%Y %I:%M:%S %p').dt.strftime('%Y%m%d%H%M%S')\n",
"df = df.sort_values(['DATA_AS_OF'])\n",
"# reset the index\n",
"df = df.reset_index(drop=True)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"doing 262doing 106doing 184doing 3doing 204doing 2doing 221doing 217doing 1doing 4doing 149\n",
"doing 150doing 223\n",
"doing 145\n",
"doing 208doing 450doing 199\n",
"doing 331doing 315doing 332doing 207doing 171doing 170doing 169\n",
"doing 440doing 325\n",
"\n",
"doing 191doing 330\n",
"doing 195doing 329\n",
"doing 345doing 410doing 347doing 344doing 213doing 399doing 398\n",
"\n",
"doing 395doing 164doing 141doing 451\n",
"\n",
"doing 140doing 202\n",
"doing 417doing 264\n",
"\n",
"doing 168doing 411\n",
"doing 406doing 364doing 298doing 190doing 263doing 211\n",
"doing 416doing 318\n",
"\n",
"doing 212\n",
"doing 448doing 311doing 319doing 388\n",
"doing 351doing 419doing 418doing 434doing 390doing 430doing 385\n",
"doing 387doing 258doing 412doing 394\n",
"\n",
"doing 413doing 433doing 431doing 259\n",
"\n",
"doing 428doing 155\n",
"doing 129doing 453doing 126\n",
"\n",
"\n",
"\n",
"doing 261\n",
"doing 154doing 153doing 338\n",
"doing 185\n",
"doing 425\n",
"doing 160doing 427doing 142doing 426\n",
"\n",
"doing 178doing 422\n",
"doing 424doing 165doing 369\n",
"doing 423\n",
"\n",
"doing 177\n",
"doing 110\n",
"\n",
"doing 172doing 167\n",
"doing 215\n",
"\n",
"doing 379doing 222\n",
"\n",
"\n",
"\n",
"\n",
"doing 383doing 257doing 265doing 324doing 380\n",
"doing 445doing 119\n",
"\n",
"\n",
"doing 137doing 124\n",
"doing 402doing 186\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"doing 349\n",
"doing 354\n",
"doing 365doing 205\n",
"doing 206\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"doing 405\n",
"\n",
"\n",
"doing 381doing 439\n",
"doing 350doing 377\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"doing 376doing 375\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"doing 295\n",
"doing 159\n",
"\n",
"\n",
"\n",
"doing 378\n",
"doing 435\n",
"doing 384\n",
"doing 441\n",
"doing 436\n",
"doing 437\n",
"doing 382\n",
"doing 316\n",
"doing 148\n",
"doing 157\n",
"doing 339\n",
"doing 432\n",
"doing 389\n",
"doing 108\n",
"doing 107\n",
"doing 225\n",
"doing 446\n",
"doing 444\n",
"doing 224\n",
"doing 442\n",
"doing 447\n",
"doing 443\n",
"doing 122\n",
"doing 123\n",
"doing 313\n"
]
}
],
"source": [
"from multiprocessing import Pool\n",
"\n",
"os.makedirs('./full_data', exist_ok=True)\n",
"os.makedirs('./slim_data', exist_ok=True)\n",
"\n",
"def process_id(id):\n",
" print(f'doing {id}')\n",
" # filter the data by id\n",
" df = a[a['ID'] == id]\n",
" \n",
" # convert DATA_AS_OF from %m/%d/%Y %I:%M:%S %p to %Y%m%d%H%M%S\n",
" df = df.sort_values(['DATA_AS_OF'])\n",
" # reset the index\n",
" df = df.reset_index(drop=True)\n",
" df.to_parquet('./full_data/id_{}.parquet'.format(id))\n",
" # get the value of BOROUGH column of first raw\n",
" borough = df['BOROUGH'].iloc[0]\n",
" link = df['LINK_NAME'].iloc[0]\n",
" length = len(df)\n",
" id_info = {id: {'borough': borough, 'link': link, 'len': length}}\n",
" df = df.drop(columns=['BOROUGH', 'LINK_NAME', 'ID','LINK_POINTS', 'STATUS'])\n",
" # rearrange the columns\n",
" df = df[['DATA_AS_OF', 'SPEED', 'TRAVEL_TIME']]\n",
" df.to_parquet('./slim_data/id_{}.parquet'.format(id))\n",
" return id_info\n",
"\n",
"with Pool() as pool:\n",
" results = pool.map(process_id, id_list)\n",
"\n",
"# Combine all the id_info dictionaries into one\n",
"id_info = {}\n",
"for result in results:\n",
" id_info.update(result)\n",
"\n",
"with open('./id_info.json', 'w') as f:\n",
" json.dump(id_info, f)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.microsoft.datawrangler.viewer.v0+json": {
"columns": [
{
"name": "index",
"rawType": "int64",
"type": "integer"
},
{
"name": "ID",
"rawType": "int64",
"type": "integer"
},
{
"name": "SPEED",
"rawType": "float64",
"type": "float"
},
{
"name": "TRAVEL_TIME",
"rawType": "int64",
"type": "integer"
},
{
"name": "STATUS",
"rawType": "int64",
"type": "integer"
},
{
"name": "DATA_AS_OF",
"rawType": "object",
"type": "string"
},
{
"name": "LINK_POINTS",
"rawType": "object",
"type": "string"
},
{
"name": "BOROUGH",
"rawType": "object",
"type": "string"
},
{
"name": "LINK_NAME",
"rawType": "object",
"type": "string"
}
],
"conversionMethod": "pd.DataFrame",
"ref": "ebc49528-2e7d-4a5d-9cb1-fc940f203bea",
"rows": [
[
"0",
"444",
"0.0",
"0",
"-101",
"19780101000000",
"40.7051105,-74.01663 40.70432,-74.01667 40.7035205,-74.016561 40.70224,-74.015841 40.7016205,-74.01527 40.701401,-74.01473 40.7012804,-74.01399 40.7012,-74.01327 40.701401,-74.012031 40.7018906,-74.00995 40.70306,-74.007731 40.70481,-74.004951 40.70656,-7",
"Manhattan",
"West St S Battery Pl - FDR N Catherine Slip"
],
[
"1",
"444",
"13.05",
"105",
"0",
"20151208152435",
"40.7051105,-74.01663 40.70432,-74.01667 40.7035205,-74.016561 40.70224,-74.015841 40.7016205,-74.01527 40.701401,-74.01473 40.7012804,-74.01399 40.7012,-74.01327 40.701401,-74.012031 40.7018906,-74.00995 40.70306,-74.007731 40.70481,-74.004951 40.70656,-7",
"Manhattan",
"West St S Battery Pl - FDR N Catherine Slip"
]
],
"shape": {
"columns": 8,
"rows": 2
}
},
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" ID | \n",
" SPEED | \n",
" TRAVEL_TIME | \n",
" STATUS | \n",
" DATA_AS_OF | \n",
" LINK_POINTS | \n",
" BOROUGH | \n",
" LINK_NAME | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 444 | \n",
" 0.00 | \n",
" 0 | \n",
" -101 | \n",
" 19780101000000 | \n",
" 40.7051105,-74.01663 40.70432,-74.01667 40.703... | \n",
" Manhattan | \n",
" West St S Battery Pl - FDR N Catherine Slip | \n",
"
\n",
" \n",
" | 1 | \n",
" 444 | \n",
" 13.05 | \n",
" 105 | \n",
" 0 | \n",
" 20151208152435 | \n",
" 40.7051105,-74.01663 40.70432,-74.01667 40.703... | \n",
" Manhattan | \n",
" West St S Battery Pl - FDR N Catherine Slip | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" ID SPEED TRAVEL_TIME STATUS DATA_AS_OF \\\n",
"0 444 0.00 0 -101 19780101000000 \n",
"1 444 13.05 105 0 20151208152435 \n",
"\n",
" LINK_POINTS BOROUGH \\\n",
"0 40.7051105,-74.01663 40.70432,-74.01667 40.703... Manhattan \n",
"1 40.7051105,-74.01663 40.70432,-74.01667 40.703... Manhattan \n",
"\n",
" LINK_NAME \n",
"0 West St S Battery Pl - FDR N Catherine Slip \n",
"1 West St S Battery Pl - FDR N Catherine Slip "
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# convert the DATA_AS_OF to datetime\n",
"\n",
"df"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "probts",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}