Skip to content

Instantly share code, notes, and snippets.

@victoriano
Created January 6, 2019 21:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victoriano/8e33eaad5b95cbb0ce17add06c556ec3 to your computer and use it in GitHub Desktop.
Save victoriano/8e33eaad5b95cbb0ce17add06c556ec3 to your computer and use it in GitHub Desktop.
Google Locations json to csv
import json
import pandas as pd
from pandas.io.json import json_normalize
map_files_dir = '.././Data_Services_Exports/Google Takeout/Location History/'
#load json object
with open(map_files_dir + "Location History.json") as f:
d = json.load(f)
df_locations = json_normalize(d['locations'])
#Get the date from Timestamp
df_locations['date'] = pd.to_datetime(df_locations['timestampMs'], unit='ms')
#Fix coordinates
df_locations['latitude'] = (df_locations['latitudeE7']/10000000)
df_locations['longitude'] = (df_locations['longitudeE7']/10000000)
#Enrich with day, hour of the day, day of the week, month of the year, quarter, year
df_locations['day_of_the_year'] = pd.to_datetime(df_locations['date']).dt.strftime('%x')
df_locations['hour_of_day'] = pd.to_datetime(df_locations['date']).dt.strftime('%H:%M')
df_locations['day_of_week'] = df_locations['date'].dt.day_name()
df_locations['weekday'] = ((df_locations['date'].dt.dayofweek) // 5 == 1).astype(float)
df_locations['month_of_year'] = df_locations['date'].dt.month_name()
df_locations['quarter'] = df_locations['date'].dt.quarter
df_locations['year'] = df_locations['date'].dt.year
header = ['date', 'day_of_the_year', 'hour_of_day', 'latitude', 'longitude', 'velocity',
'day_of_week', 'weekday', 'month_of_year', 'accuracy', 'quarter', 'year', 'verticalAccuracy']
df_locations.to_csv("/Users/graphext/Desktop/location_history.csv", columns = header, index = False)
df_locations.head(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment