Export Dataset to CSV#

This notebook shows an example of how to export a dataset to a csv file. It assumes you found the dataset using techniques shown in finding_datasets.ipynb and loaded the dataset using loading_datasets.ipynb

[1]:
import openpolicedata as opd
[2]:
# To access the data, create a source using a Source Name (usually a police department name). There is an optional state input to clarify ambiguities.
# We will use the above cell's information for Maryland to choose the agency "Montgomery County" which we select for the source_name

src = opd.Source(source_name="Montgomery County", state="Maryland")
src.datasets.head()
[2]:
State SourceName Agency AgencyFull TableType coverage_start coverage_end last_coverage_check Description source_url readme URL Year DataType date_field dataset_id agency_field min_version query
480 Maryland Montgomery County Montgomery County Montgomery County Police Department COMPLAINTS 2013-10-24 2024-05-06 05/10/2024 This dataset contains allegations brought to t... https://data.montgomerycountymd.gov/Public-Saf... <NA> data.montgomerycountymd.gov MULTIPLE Socrata created_dt usip-62e2 <NA> <NA> NaN
481 Maryland Montgomery County Montgomery County Montgomery County Police Department CRASHES - INCIDENTS 2015-12-20 2024-01-03 05/10/2024 general information about each collision and d... https://data.montgomerycountymd.gov/Public-Saf... <NA> data.montgomerycountymd.gov MULTIPLE Socrata crash_date_time bhju-22kf <NA> 0.4 NaN
482 Maryland Montgomery County Montgomery County Montgomery County Police Department CRASHES - NONMOTORIST 2015-03-23 2023-12-31 05/10/2024 information on non-motorists (pedestrians and ... https://data.montgomerycountymd.gov/Public-Saf... <NA> data.montgomerycountymd.gov MULTIPLE Socrata crash_date_time n7fk-dce5 <NA> 0.5 NaN
483 Maryland Montgomery County Montgomery County Montgomery County Police Department CRASHES - SUBJECTS 2015-06-30 2024-01-03 05/10/2024 information on motor vehicle operators (driver... https://data.montgomerycountymd.gov/Public-Saf... <NA> data.montgomerycountymd.gov MULTIPLE Socrata crash_date_time mmzv-x632 <NA> 0.4 NaN
484 Maryland Montgomery County Montgomery County Montgomery County Police Department INCIDENTS 2017-04-02 2024-05-10 05/10/2024 list of Police Dispatched Incidents records https://data.montgomerycountymd.gov/Public-Saf... <NA> data.montgomerycountymd.gov MULTIPLE Socrata start_time 98cc-bc7d <NA> <NA> NaN
[3]:
# Load traffic stop data for 2021
t = src.load(table_type='TRAFFIC STOPS', year=2021)
[4]:
# Show the first 5 rows of the table
t.table.head(n=5)
# Now you are ready for analyzing the data in the table t.
[4]:
geometry seq_id date_of_stop time_of_stop agency subagency description location latitude longitude ... driver_state dl_state arrest_type search_conducted search_outcome search_reason_for_stop search_disposition search_reason search_type search_arrest_reason
0 POINT (-77.13047 39.01268) f08d0293-6ade-4802-84c1-4b7b1a707245 2021-01-01 03:12:00 MCP 2nd District, Bethesda RECKLESS DRIVING VEHICLE IN WANTON AND WILLFUL... IFO 9609 SINGLETON DR 39.0126813333333 -77.130466 ... MD MD A - Marked Patrol NaN NaN NaN NaN NaN NaN NaN
1 POINT (-77.13047 39.01268) f08d0293-6ade-4802-84c1-4b7b1a707245 2021-01-01 03:12:00 MCP 2nd District, Bethesda FAILURE OF VEH. DRIVER IN ACCIDENT TO LOCATE A... IFO 9609 SINGLETON DR 39.0126813333333 -77.130466 ... MD MD A - Marked Patrol NaN NaN NaN NaN NaN NaN NaN
2 POINT (-77.13047 39.01268) f08d0293-6ade-4802-84c1-4b7b1a707245 2021-01-01 03:12:00 MCP 2nd District, Bethesda NEGLIGENT DRIVING VEHICLE IN CARELESS AND IMPR... IFO 9609 SINGLETON DR 39.0126813333333 -77.130466 ... MD MD A - Marked Patrol NaN NaN NaN NaN NaN NaN NaN
3 POINT (-77.13047 39.01268) f08d0293-6ade-4802-84c1-4b7b1a707245 2021-01-01 03:12:00 MCP 2nd District, Bethesda FAILURE OF VEH. DRIVER TO STOP AFTER UNATTENDE... IFO 9609 SINGLETON DR 39.0126813333333 -77.130466 ... MD MD A - Marked Patrol NaN NaN NaN NaN NaN NaN NaN
4 POINT (-77.13047 39.01268) f08d0293-6ade-4802-84c1-4b7b1a707245 2021-01-01 03:12:00 MCP 2nd District, Bethesda FAILURE OF VEH. DRIVER INVOLVED IN ACCIDENT TO... IFO 9609 SINGLETON DR 39.0126813333333 -77.130466 ... MD MD A - Marked Patrol NaN NaN NaN NaN NaN NaN NaN

5 rows × 43 columns

[5]:
import os
cwd = os.getcwd()
csv_filepath = cwd

print(f"The CSV file will be written to {csv_filepath}. Make sure this path is okay before running the next cell. If the path is not okay then modify csv_filepath.")

The CSV file will be written to c:\Users\matth\repos\openpolicedata\docs\source\examples\opd-examples. Make sure this path is okay before running the next cell. If the path is not okay then modify csv_filepath.
[6]:
# Save to CSV. To specify a custom filename, set the filename input
csv_written_filename=t.to_csv(output_dir=csv_filepath)
print(f"The CSV file was written to {csv_written_filename}.")
The CSV file was written to c:\Users\matth\repos\openpolicedata\docs\source\examples\opd-examples\Maryland_Montgomery_County_TRAFFIC_STOPS_2021.csv.
[7]:
# To load data back in from CSV, create a new source and use load_from_csv
# load_from_csv usage is similar to load except for the output_dir
# input
src = opd.Source(source_name="Montgomery County", state="Maryland")
t = src.load_from_csv(year=2021, table_type='TRAFFIC STOPS', output_dir=csv_filepath)
t.table.head()
[7]:
geometry seq_id date_of_stop time_of_stop agency subagency description location latitude longitude ... driver_state dl_state arrest_type search_conducted search_outcome search_reason_for_stop search_disposition search_reason search_type search_arrest_reason
0 POINT (-77.130466 39.0126813333333) f08d0293-6ade-4802-84c1-4b7b1a707245 2021-01-01 03:12:00 MCP 2nd District, Bethesda RECKLESS DRIVING VEHICLE IN WANTON AND WILLFUL... IFO 9609 SINGLETON DR 39.012681 -77.130466 ... MD MD A - Marked Patrol NaN NaN NaN NaN NaN NaN NaN
1 POINT (-77.130466 39.0126813333333) f08d0293-6ade-4802-84c1-4b7b1a707245 2021-01-01 03:12:00 MCP 2nd District, Bethesda FAILURE OF VEH. DRIVER IN ACCIDENT TO LOCATE A... IFO 9609 SINGLETON DR 39.012681 -77.130466 ... MD MD A - Marked Patrol NaN NaN NaN NaN NaN NaN NaN
2 POINT (-77.130466 39.0126813333333) f08d0293-6ade-4802-84c1-4b7b1a707245 2021-01-01 03:12:00 MCP 2nd District, Bethesda NEGLIGENT DRIVING VEHICLE IN CARELESS AND IMPR... IFO 9609 SINGLETON DR 39.012681 -77.130466 ... MD MD A - Marked Patrol NaN NaN NaN NaN NaN NaN NaN
3 POINT (-77.130466 39.0126813333333) f08d0293-6ade-4802-84c1-4b7b1a707245 2021-01-01 03:12:00 MCP 2nd District, Bethesda FAILURE OF VEH. DRIVER TO STOP AFTER UNATTENDE... IFO 9609 SINGLETON DR 39.012681 -77.130466 ... MD MD A - Marked Patrol NaN NaN NaN NaN NaN NaN NaN
4 POINT (-77.130466 39.0126813333333) f08d0293-6ade-4802-84c1-4b7b1a707245 2021-01-01 03:12:00 MCP 2nd District, Bethesda FAILURE OF VEH. DRIVER INVOLVED IN ACCIDENT TO... IFO 9609 SINGLETON DR 39.012681 -77.130466 ... MD MD A - Marked Patrol NaN NaN NaN NaN NaN NaN NaN

5 rows × 43 columns