Skip to content

Commit

Permalink
Add Excel route model conversion function
Browse files Browse the repository at this point in the history
  • Loading branch information
landont168 committed Feb 4, 2024
1 parent d0cbf83 commit 8a70b8a
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions etl/etl_routemodel/etl_route.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Note: Need to 'pip install openpyxl'
import pandas as pd
from pathlib import Path

def normalize_route_segments(file_name):
combined_segments_df = pd.DataFrame()
segment_sheets = pd.read_excel(file_name, engine="openpyxl", sheet_name=None, header=2)

# Add segment and indicate loop segments
for segment_name, segment_df in segment_sheets.items():
segment_df = segment_df.loc[:, ~segment_df.columns.str.contains("^Unnamed")]
segment_df["Segment"] = segment_name

if segment_name.lower().find("loop") != -1:
segment_df["is_loop"] = True
else:
segment_df["is_loop"] = False
combined_segments_df = pd.concat([combined_segments_df, segment_df], ignore_index=True)

return combined_segments_df


def save_to_csv(df, excel_file_name):
csv_file_name = Path(excel_file_name).with_suffix(".csv")
df.to_csv(csv_file_name, index=False)
return df


def main():
# Set Excel file name
excel_file_name = "route-steps-2022.xlsx"

# Normalize route steps
combined_segments_df = normalize_route_segments(excel_file_name)

# Save combined DataFrame to CSV
save_to_csv(combined_segments_df, excel_file_name)


if __name__ == "__main__":
main()

0 comments on commit 8a70b8a

Please sign in to comment.