Skip to content

Instantly share code, notes, and snippets.

@erichannell
Created April 25, 2018 21:25
Show Gist options
  • Save erichannell/dcfcbe24649f8efc369f9a30b07e15ce to your computer and use it in GitHub Desktop.
Save erichannell/dcfcbe24649f8efc369f9a30b07e15ce to your computer and use it in GitHub Desktop.
convert data from takeout.google.com from json to csv format
import pandas as pd
df_gps = pd.read_json('input.json')
# parse lat, lon, and timestamp from the dict inside the locations column
df_gps['lat'] = df_gps['locations'].map(lambda x: x['latitudeE7'])
df_gps['lon'] = df_gps['locations'].map(lambda x: x['longitudeE7'])
df_gps['timestamp_ms'] = df_gps['locations'].map(lambda x: x['timestampMs'])
# convert lat/lon to decimalized degrees and the timestamp to date-time
df_gps['lat'] = df_gps['lat'] / 10.**7
df_gps['lon'] = df_gps['lon'] / 10.**7
df_gps['timestamp_ms'] = df_gps['timestamp_ms'].astype(float) / 1000
df_gps['datetime'] = df_gps['timestamp_ms'].map(lambda x: dt.fromtimestamp(x).strftime('%Y-%m-%d %H:%M:%S'))
date_range = '{}-{}'.format(df_gps['datetime'].min()[:4], df_gps['datetime'].max()[:4])
df_gps = df_gps.drop(labels=['locations', 'timestamp_ms'], axis=1, inplace=False)
print ("%s rows in file" % (len(df_gps.index)))
df_gps.to_csv("output.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment