Story hunting in birth, death data

Tracking the U.S. government’s annual count of births and deaths is one of my little obsessions. I keep annual totals in a spreadsheet and look forward to observing the trends with each new year of data.

This most basic of demographics can tell us much about a nation’s past—and its unfolding future.

For example, the CDC’s provisional 2018 U.S. birth data released in May 2019 showed that births in the U.S. dropped for a fourth year in a row, to the lowest level in 32 years. In a story for The Wall Street Journal, we encapsulated the trend and what demographers point to as likely causes: sharp declines in the teen birth rate, increased use of longer-acting contraceptives, and more women in the workforce delaying childbirth, among others.

Continue…

Setting up Python in Windows 10

Installing Python under Windows 10 is fairly easy as long as you set up your system environment correctly. Here’s your quick guide:

  1. Visit the official Python download page and grab the Windows installer for the latest version of Python 3.
  2. Navigate to the folder where you saved the installation file. Right-click the installer and select “Run as Administrator.” Click “Yes” when Windows asks if you want the program to make changes to your computer. 
  3. The next dialog asks whether you want to “Install Now” or “Customize Installation.” First, check the boxes that say “Install launcher for all users” and “Add Python 3.9 to PATH”. Then, choose “Customize Installation.”
  4. On the next screen, check all boxes under “Optional Features.” Click next.
  5. Under “Advanced Options,” set the location where you want to install Python. I like to use a location I can easily find, such as:
C:\Python39

That will remind me that I’ve installed Python 3.9.

Also under “Advanced Options,” check the following boxes:

  • “Install for all users”
  • “Associate files with Python”
  • “Create shortcuts for installed applications”
  • “Add Python to environment variables”
  • “Precompile standard library”
  1. Click through the dialog to finish the installation and exit the installer.

Testing Your Install

Now, to launch the Python interpreter from the command line, you can open a command prompt (Start Menu > Windows System > Command Prompt) and type:

python

That will load the Python interpreter:

Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Because of the settings you chose during the install, you can now run this interpreter — and, more important, a script — from any directory on your system.

Type exit() and hit Return to exit the interpreter and get back to a C: prompt.

Optional: Set up useful Python packages

Python 3 comes with the package installer pip already in place, which makes it super easy to add useful packages to your Python installation. The syntax is this (replace package_name_here with the name of a package you want to install):

pip install package_name_here

Let’s add a couple of must-have utilities for web scraping: Requests and BeautifulSoup. You can use pip to install them all with one command:

pip install beautifulsoup4 requests

csvkit, which I covered here, is a great tool for dealing with comma-delimited text files. Add it:

pip install csvkit

You’re now set to get started using and learning Python under Windows 10. If you’re looking for a guide, start with the Official Python tutorial.


Like What You Read Here?
My book Practical SQL: A Beginner’s Guide to Storytelling with Data from No Starch Press offers a step-by-step guide learning SQL and working with relational databases. Learn how to wrangle the everyday data you encounter to gain meaningful insights.

Analyzing Shapefile Data with PostgreSQL

This post, on how to analyze an Esri shapefile with PostgreSQL, is adapted from material in my book Practical SQL: A Beginner’s Guide to Storytelling with Data from No Starch Press.


Spend some time digging into geographic information systems (GIS) and soon enough you’ll encounter a shapefile. It’s a GIS file type developed by mapping software firm Esri for use in its popular ArcGIS platform. A shapefile contains the geometric information to describe a shape—a river, road, lake, or town boundary, for example—plus metadata about the shape, such as its name.

Because the shapefile has become a de facto standard for publishing GIS data, other applications and software libraries use shapefiles too, such as the open source QGIS.

While researching GIS topics for a chapter in my book, Practical SQL, I learned that it’s easy to import a shapefile into a PostGIS-enabled PostgreSQL database. The information that describes each shape is stored in a column of data type geometry, and so you can run spatial queries to calculate area, distances, intersections of objects, and more.

Here’s a quick exercise, adapted from the book.

Continue…

DC PostgreSQL User Group, June 2018

Many thanks to Stephen Frost of Crunchy Data and Brad Sneade of LiveSafe for inviting me to speak about my book Practical SQL at the DC PostgreSQL User Group in early June! The night featured good food, fun conversations, and tales from me on how a journalist came to write a book about PostgreSQL and data analysis.

I shared tips I picked up along the way on using PostGIS, crosstabs, statistics functions, and Python within PostgreSQL—all topics I cover in the book.

The DC PostgreSQL Users Group features a warm, inviting atmosphere. Check out its Meetup page and consider stopping in if you’re in the region.

A few tweets from the evening:

Next time, I will bring a bigger screen …

‘Practical SQL’ Available in Bookstores!

I’m thrilled to say that Practical SQL: A Beginner’s Guide to Storytelling with Data is officially released today! The title is published by No Starch Press and distributed via Penguin Random House, which means you can find it wherever books are sold.

From the description:

Practical SQL is an approachable and fast-paced guide to SQL (Structured Query Language), the standard programming language for defining, organizing, and exploring data in relational databases. The book focuses on using SQL to find the story your data tells, with the popular open-source database PostgreSQL and the pgAdmin interface as its primary tools.

Practical SQL Anthony DeBarrosMuch of Practical SQL is based on the years I spent in newsrooms, including USA TODAY, poring over data sets in search of a story. SQL-driven databases were a central part of my toolkit, allowing me to organize, clean, and find meaning in data sets ranging from a handful of rows up to millions of records across dozens of tables. Today, the language is still widely used, powering thousands upon thousands of software applications.

Please check out the bundle from No Starch that includes a print copy plus ebook versions (PDF, .mobi, and .epub). No Starch Press is a thoughtful company that supports the open source software community, so you can feel good backing them. No Starch often runs promotions, so follow the company on Twitter or get their newsletter for deals.

Of course, you can also order the book through Amazon, and copies should be on shelves at Barnes & Noble or your favorite independent local bookstore.

In coming weeks, I’ll announce some special giveaways, sharing tips from the book, and booking some in-store appearances. Stay tuned.