-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSON_Import.py
More file actions
88 lines (70 loc) · 2.71 KB
/
JSON_Import.py
File metadata and controls
88 lines (70 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import json
import pandas as pd
import streamlit as st
from datetime import datetime
# Set page layout
st.set_page_config(
page_title="Travel",
page_icon="🌍",
layout="wide",
initial_sidebar_state="expanded",
)
@st.cache(allow_output_mutation=True)
def read_json(json_file):
# read the json file
with open(json_file, 'r') as f:
data = json.loads(f.read())
# Flattern dataframe
df = pd.json_normalize(data, record_path =['locations'])
# create a dataframe from the json file
#df = pd.DataFrame(data)
# return the dataframe
return df
# create a function to read timestampms as a column and convert to datetime
def convert_timestampms(df):
# convert the timestampms column to datetime
df['timestampMs'] = pd.to_datetime(df['timestampMs'], unit='ms')
df['date'] = df['timestampMs'].dt.date
df['Time'] = df['timestampMs'].dt.time
# return the dataframe
return df
# create a function to convert latitudeE7 and longitudeE7 to decimal degrees
def convert_latlong(df):
# convert the latitudeE7 and longitudeE7 to decimal degrees
df['latitude'] = df['latitudeE7'] / 10000000
df['longitude'] = df['longitudeE7'] / 10000000
# return the dataframe
return df
# Variable/ path declaration
json_file = 'C:/Users/ashis/OneDrive/Desktop/BI/Takeout/Location History/'
json_file = json_file + 'Location_History.json'
df = read_json(json_file)
df = convert_timestampms(df)
df = convert_latlong(df)
print(f'The shape of the imported data is {df.shape}')
print(df.head())
# filtering the data for WIFI
df = df[df['source'] != 'WIFI']
# Keeping required columns
columns = ['date', 'Time', 'latitude', 'longitude', 'accuracy', 'source', 'velocity', 'altitude', 'platform', 'platformType']
df_2016 = df.loc[df['date'] > pd.to_datetime('2016-01-01'), columns].reset_index(drop=True)
print(f'The shape of the imported data is {df_2016.shape}')
st.title("🌍 Travels Exploration")
# Calculate the timerange for the slider
min_ts = min(df_2016["date"])
max_ts = max(df_2016["date"])
st.sidebar.subheader("Inputs")
min_selection, max_selection = st.sidebar.slider(
"Timeline", min_value=min_ts, max_value=max_ts, value=[min_ts, max_ts]
)
# Toggles for the feature selection in sidebar
show_detailed_months = st.sidebar.checkbox("Show Detailed Split per Year")
show_code = st.sidebar.checkbox("Show Code")
# Filter Data based on selection
st.write(f"Filtering between {min_selection} & {max_selection}")
travel_data = df_2016[
(df_2016["date"] >= min_selection) & (df_2016["date"] <= max_selection)
]
st.write(f"Data Points: {len(df)}")
# Plot the GPS coordinates on the map
st.map(travel_data)