Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@abehmiel
Created May 23, 2017 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abehmiel/d3efb667f6f2724410ca7227379d2674 to your computer and use it in GitHub Desktop.
Save abehmiel/d3efb667f6f2724410ca7227379d2674 to your computer and use it in GitHub Desktop.
Fitbit cleaner
import pandas as pd
from datetime import datetime, date
"""
Cleaning time!
This script will clean data downloaded from the fitbit website.
A little bit of preprocessing is useful. First, Separate the activities
and sleep data into different csv files and make sure there's only
one header row in each. Multiple months may be concatenated together,
since the fitbit website only allows you to download 31 days at a time.
You should name the files "activities.csv" and "sleep.csv" in the working directory.
I haven't done a cleaning script for body/food data yet. In the end, you will
receive two data frames with an additional column for day of the week, and where the
date is a datetime object, which is a little more easy to compute with.
"""
activities_data = pd.read_csv("/XXXX/activities.csv")
sleep_data = pd.read_csv("/XXXX/sleep.csv")
def zap_zeros_activities(df):
"""Removes elements from dataframe with missing data"""
return df[df["Activity Calories"] != 0]
def zap_zeros_sleep(df):
"""Removes elements from dataframe with missing data"""
return df[df["Time in Bed"] != 0]
activities_data = zap_zeros_activities(activities_data)
sleep_data = zap_zeros_sleep(sleep_data)
#clean string data to datetime object in new column
def time_transform(myDate):
return datetime.strptime(myDate,'%Y/%m/%d')
# get day of week of a datetime object
def get_day_of_week(myDate):
return myDate.strftime('%A')
activities_data['Date'] = activities_data['Date'].apply(time_transform)
sleep_data['Date'] = sleep_data['Date'].apply(time_transform)
activities_data['Weekday'] = activities_data['Date'].apply(get_day_of_week)
sleep_data['Weekday'] = sleep_data['Date'].apply(get_day_of_week)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment