Car Rental Scheduling and compatibility - Python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Car Rental Scheduling and compatibility - Python (/thread-20146.html) |
Car Rental Scheduling and compatibility - Python - Alfm - Jul-29-2019 The code only validating one car model, one compatibility model and all the booking schedule. I don’t know how to write the code to validate all car compatibility and schedule. The goal is to reuse the car model and optimize the booking request by date, time and all car compatibility. Each day has several sessions. We currently have 8 car models; one car model is considered compatible with every single booking or needs. And to ensure that the rental runs smoothly, each session requires a car model assign. I need to check availability of the car model by date Check the all compatibility by each model. Model Code= Car Brand Model Session= Identify each individual future booking Event Date= Future Booking Session Start Time= Future Booking Start Time End Time= Future Booking End Time Car Type- Model type Compatibility with model type and schedule Letter A, B, C, D, E, F, G = 1 means its compatible with the booking = 0 means isn’t compatible with the booking import pandas as pd # In[2]: df = pd.read_csv(r'C:\Users\XXXXXX\Documents\Car_Rental.csv') # In[3]: listA =['ACU','BMW','GM','FIA','JEE','FIA','HON','HYU','JEE','KTE'] listC = ['ACU','BMW','FIA','JEE','FIA','HON','HYU','JEE','KTE'] listD = ['ACU','BMW','FIA','JEE','FIA','HON','HYU','JEE'] listE = ['ACU','BMW','FIA'] listF =['ACU','BMW','FIA','JEE'] listG = ['ACU','BMW','GM','FIA','JEE'] df['A'] = df['Model Code'].apply(lambda x: 1 if x in listA else 0) df['B'] = df['Model Code'].apply(lambda x: 1 if x in listA else 0) df['C'] = df['Model Code'].apply(lambda x: 1 if x in listC else 0) df['D'] = df['Model Code'].apply(lambda x: 1 if x in listD else 0) df['E'] = df['Model Code'].apply(lambda x: 1 if x in listE else 0) df['F'] = df['Model Code'].apply(lambda x: 1 if x in listF else 0) df['G'] = df['Model Code'].apply(lambda x: 1 if x in listG else 0) df # In[4]: def conflicts_eventdates_Modelcodes(x,y): du = df du['Start DateTime'] = pd.to_datetime(du['Start DateTime']) du['End Date/Time'] = pd.to_datetime(du['End Date/Time']) du['Event Date'] = pd.to_datetime(du['Event Date']) x=pd.to_datetime(x) start_times=[] end_times=[] Sessions= [] count = 0 dux = du[du[y] ==1] #print(dua) Sports =[] dux = dux[dux['Event Date'] == x] for i,j,k in zip(dux['Start DateTime'],dux['End Date/Time'],dux['Model Session']): count= count+1; start_times.append(i) end_times.append(j) Sessions.append(k) events = list(zip(start_times,end_times)) times = [] for event in events: startTime, endTime = event times.append((startTime, 'start')) times.append((endTime, 'end')) #print(events) times = sorted(times) count = 0 maxCount = 0 for time in times: if time[1] == 'start': count += 1 else: count -= 1 maxCount = max(count, maxCount) return maxCount # In[5]: conflicts_eventdates_Modelcodes('2020-07-23','A') # In[6]: df[df['A'] ==1]['Model Code'].unique() # In[7]: def conflicts_eventdates_Modelcodes1(x): du = df du['Start DateTime'] = pd.to_datetime(du['Start DateTime']) du['End Date/Time'] = pd.to_datetime(du['End Date/Time']) du['Event Date'] = pd.to_datetime(du['Event Date']) #x=pd.to_datetime(x) start_times=[] end_times=[] Sessions= [] count = 0 #dux = du[du[y] ==1] #print(dua) Sports =[] dux = du dux = dux[dux['Model Code'] == x] for i,j,k in zip(dux['Start DateTime'],dux['End Date/Time'],dux['Model Session']): count= count+1; start_times.append(i) end_times.append(j) Sessions.append(k) events = list(zip(start_times,end_times)) times = [] for event in events: startTime, endTime = event times.append((startTime, 'start')) times.append((endTime, 'end')) #print(events) times = sorted(times) count = 0 maxCount = 0 for time in times: if time[1] == 'start': count += 1 else: count -= 1 maxCount = max(count, maxCount) return maxCount # In[8]: conflicts_eventdates_Modelcodes1('BMW') RE: Car Rental Scheduling and compatibility - Python - scidam - Jul-30-2019 Not sure about the code, it is hard to understand, at least, for me. However, you can improve some steps, e.g. inclusion check could be done using .isin method, e.g.df['B'] = df['Model Code'].isin(ListB) # optionally, you can convery bools to ints by applying `.astype(int)` |