Analyzing Apple Health Data with Python

Vinayak Gaur
4 min readFeb 6, 2022

--

Our smartphones are incredible mini-trackers that can be used for self-improvement and self-transformation.

If you are an Apple user, then your iPhone has been tracking your steps and a host of other health metrics. Some are directly recorded by the phone. Others are logged via other health apps that store their data into the Apple Health repository. If you also regularly wear an Apple Watch during the day, during workouts and at night, then you have even more data, like Heart Rate, VO2 Max, and possibility even Sleep.

This article will walk you through how to export your Apple Health Data from Apple Health app and then do some data analysis with Python.

Extracting Apple Health Data

In order to export the raw data, go to “Health” app on your iPhone, click on account icon on the top right and then “Export All Health Data”. This might take some time. Then you have to transfer this export data from your device to your PC/laptop via AirDrop, Email or any other preferable method.

Exploring Data

Once you unzip the raw data from Apple Health, you will discover there are two files: export.xml & export_cda.xml, and two folders: electrocardiograms & workout-routes.

The electrocardiograms folder contains your ECG data recorded by your Apple Watch, whereas workout-routes folder contains your workout location data. All other data such as Heartrate, Activity, Step count, Sleep Analysis, etc. can be retrieved from export.xml file.

For our analysis we will be working with export.xml file and will be using Python 3 & JupyterLab.

First, we will start with converting this XML file to a human readable CSV format.

Open JupyterLab in you favorite web browser and upload the export.xml file in the same directory where you will be saving your Python3 notebooks. Also, upload the following apple-health-data-parser.py file to the same directory. This file consists of several functions to parse health export data.

Run the following code to extract the data from XML file.

This process may take several minutes to run depending on the size of your Apple Health directory. After the completing the process, you should have several new files. Each file should include a CSV export of that health metric. This data should be both well-structured and quite verbose, meaning it contains everything originally stored in Apple.

Analyzing data

Start with importing libraries

Extract and convert the date/time elements from UTC to PST (you can change this your time zone)

In this article I will be showing analysis of Steps Count metrics, however you can use the code (with some alterations) to do analysis on other metrics as well.

Reading data from StepCount.csv file

Parsing date/time elements for detail analysis.

Here is the sneak peek of the data.

Grouping data by year by device

Rolling Average

Steps by Day of Week

Steps per Year

Steps by Hour of Day

Summary

In this article, we looked at how to export, parse and analyze your own personal health data from your Apple device. I would highly encourage everyone to further analyze other related metrics such as Exercise time, Body mass, Sleep analysis, Heart rate etc. by using the similar code above (with slight adjustments).

Thanks for reading!

Helpful Resources

GitHub repo: https://github.com/vinayakgaur/Apple-Health-Data-Analysis

--

--