IAMTFRMZA commited on
Commit
581bd58
·
verified ·
1 Parent(s): 8601d12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
app.py CHANGED
@@ -23,15 +23,21 @@ def load_tab(sheet_name):
23
 
24
  # GPS calculations
25
  def calculate_gps_data(df):
26
- df = df.sort_values(['Date', 'Time'])
27
  df[['Latitude', 'Longitude']] = df['Location'].str.split(', ', expand=True).astype(float)
28
- df['Kms Travelled'] = df[['Latitude', 'Longitude']].shift().apply(
29
- lambda row: geodesic((row['Latitude'], row['Longitude']), (row.name[0], row.name[1])).km if pd.notnull(row['Latitude']) else 0,
30
- axis=1)
31
- df['Duration Between Calls (min)'] = df[['Date', 'Time']].apply(
32
- lambda row: pd.to_datetime(row['Date'] + ' ' + row['Time']), axis=1
33
- ).diff().dt.total_seconds().div(60)
34
- df.fillna({'Kms Travelled': 0, 'Duration Between Calls (min)': 0}, inplace=True)
 
 
 
 
 
 
35
  return df
36
 
37
  # Load and process Field Sales data
 
23
 
24
  # GPS calculations
25
  def calculate_gps_data(df):
26
+ df = df.sort_values(['Date', 'Time']).reset_index(drop=True)
27
  df[['Latitude', 'Longitude']] = df['Location'].str.split(', ', expand=True).astype(float)
28
+
29
+ df['Kms Travelled'] = 0.0
30
+ df['Duration Between Calls (min)'] = 0.0
31
+
32
+ for i in range(1, len(df)):
33
+ prev_coords = (df.at[i-1, 'Latitude'], df.at[i-1, 'Longitude'])
34
+ current_coords = (df.at[i, 'Latitude'], df.at[i, 'Longitude'])
35
+ df.at[i, 'Kms Travelled'] = geodesic(prev_coords, current_coords).km
36
+
37
+ prev_time = pd.to_datetime(df.at[i-1, 'Date'] + ' ' + df.at[i-1, 'Time'])
38
+ current_time = pd.to_datetime(df.at[i, 'Date'] + ' ' + df.at[i, 'Time'])
39
+ df.at[i, 'Duration Between Calls (min)'] = (current_time - prev_time).total_seconds() / 60.0
40
+
41
  return df
42
 
43
  # Load and process Field Sales data