This repository contains materials for "Data Wrangling With Python," a hands-on session presented at the 2017 Investigative Reporters and Editors NICAR conference in Jacksonville, Fla.
The code covers basic import, transformation, and export of data from CSV files. Output formats include JSON and SQL table creation statements.
This code was written and taught on MacOS Sierra using Python 3.6; it should work fine on Python 2.7 as well as Python 3.5+. It's not been tested on Windows. I recommend setting up a virtual environment and then installing the following dependencies:
Here are the steps:
- Open your terminal and navigate to the Desktop:
cd ~/Desktop
- Clone this repository:
git clone https://github.com/anthonydb/data-wrangling-python-nicar-2017.git
- Change to the project directory you just cloned:
cd data-wrangling-python-nicar-2017/
- Set up your virtual environment. We'll call it 'nicar':
virtualenv nicar
source nicar/bin/activate
- Install your dependencies:
pip install agate csvkit requests jupyter
- Now you can run Jupyter Notebook:
jupyter notebook
-
Jupyter Notebook will open in your default browser on
localhost:
, and you will see a list of files in the directory. The most important file for this exercise is the Census 2010 data contained inus_counties_2010.csv
. -
Next, using the "New" menu at the top right of the Jupyter Notebook app, create a new notebook matching the version of Python on your system.
-
Now, you can paste in the code found in the .ipynb file in this repository and run it to follow the tutorial.
-
When you're done, run
ctrl+c
in the terminal to close Jupyter Notebook. Thendeactivate
to exit your virtual environment.
You can run the commands in the file csvkit-notes.md in your terminal. Make sure the virtual environment is activated.
This NICAR 2017 hands-on class was loosely inspired by the structure of a NICAR 2016 class taught by Jacqueline Kazil and Abraham Epton.
Suggested resources to learn more about Python and data:
- CSV File Reading and Writing: Python docs
- JSON encoder and decoder: Python docs
- The Hickhiker's Guide to Python
- Data Wrangling With Python by Jacqueline Kazil and Katharine Jarmul