因此,如果退出日期在一周范围内,那么我需要返回布尔值True或false,这样我就可以继续求和给定退出日期的预订值
week_start week_end
0 2019-05-01 2019-05-08
1 2019-05-08 2019-05-15
2 2019-05-15 2019-05-22
3 2019-05-22 2019-05-29
4 2019-05-29 2019-06-05
week_start datetime64[ns]
week_end datetime64[ns]
如果退出总和等于^^
,则以下退出日期是否在上述给定的周内范围内Exit_date = [ '2019-05-19','2019-05-27', '2019-05-26', '2019-05-28', '2019-05-27','2019-05-27', '2019-05-22', '2019-05-18', '2019-05-25', '2019-05-25', '2019-05-17', ' 2019-05-25']
booking_cost = ['113.3250','68.3250', '62.4900','80.9917', '79.9900', '41.6600', '50.8250','41.6600', '50.8250','68.3200','68.3200','114.9920']
data ={'Exit_date': Exit_date, 'booking_cost': booking_cost}
Exit_df = pd.DataFrame(data, columns=['Exit_date','booking_cost' ]) # exit date date frame
Exit_df['Exit_date'] = pd.to_datetime(Exit_df['Exit_date'])
print(Exit_df.head())
years = int('2019') #yyyy
months = int('05') #mm
dayss = int('01') #dd
Start_Date = datetime.date(years, months, dayss)#user selects date
n = [0,1,2,3,4]
week_start=[]
week_end=[]
for elements in n: #loop to create 4 week period from selected date
week_n_start = Start_Date
week_n_end = Start_Date + timedelta(days=7)
week_start.append(week_n_start)
week_end.append(week_n_end)
Start_Date=week_n_end
user_dates = {'week_start' : week_start , 'week_end' :week_end}
daterange_df = pd.DataFrame(user_dates) # creates data frame of 4 week period
daterange_df['week_start'] = pd.to_datetime(daterange_df['week_start'])
daterange_df['week_end'] = pd.to_datetime(daterange_df['week_end'])
print(daterange_df)
因此,如果退出日期在星期范围内,那么我将要对预订金额求和...像这样
Total_Sales =0
if daterange_df['week_start'] <= parking_master['Exit_Date'] <= daterange_df['week_end'] :
Total_Sales=+Exit_df['Booking Cost']
print(Total_Sales)
谢谢
答案 0 :(得分:1)
在列表理解中将Series.between
与sum
一起用于计数True
s-匹配的Exit_Date
值:
z = zip(daterange_df['week_start'], daterange_df['week_end'])
daterange_df['count'] = [parking_master['Exit_Date'].between(s, e).sum() for s, e in z]
print (daterange_df)
week_start week_end count
0 2019-05-01 2019-05-08 0
1 2019-05-08 2019-05-15 0
2 2019-05-15 2019-05-22 4
3 2019-05-22 2019-05-29 12
4 2019-05-29 2019-06-05 0