Last active
March 7, 2024 08:37
-
-
Save thomaswilley/5079f1106b1ddf2c71b6 to your computer and use it in GitHub Desktop.
Get Apple Health data as Pandas DataFrame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get Apple Health data as Pandas DataFrame | |
# === | |
# pre-reqs: python3, lxml, pandas | |
# to get started: | |
# export and mail yourself your data following steps within the Health app on iPhone | |
# download and unzip contents of exported zip file; find path to export.xml and set path_to_exportxml below | |
import pandas as pd | |
import xml.etree.ElementTree | |
import datetime | |
path_to_exportxml = "<path to apple health's export.xml>" | |
def iter_records(healthdata): | |
healthdata_attr = healthdata.attrib | |
for rec in healthdata.iterfind('.//Record'): | |
rec_dict = healthdata_attr.copy() | |
rec_dict.update(healthdata.attrib) | |
for k, v in rec.attrib.items(): | |
if 'date' in k.lower(): | |
rec_dict[k] = datetime.datetime.strptime(v, '%Y-%m-%d %H:%M:%S %z') | |
else: | |
rec_dict[k] = v | |
yield rec_dict | |
e = xml.etree.ElementTree.parse(path_to_exportxml).getroot() | |
df = pd.DataFrame(list(iter_records(e))) | |
df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Alicia, sure. Let me see if I can help. Can you send me an email at tw@increment.me and we can follow up offline by email?