我有一份司机旅行日记的数据集。对于每次旅行,csv文件中都有相关的开始时间,结束时间和星期几。没有与旅行相关的日期。
我现在已经将数据导入python,其中每个开始时间和结束时间都附加了工作日,如下所示:
time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=23,
tm_min=45, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)
print journey['BeginTime'][2].tm_wday, journey['BeginTime'][2].tm_hour
周一返回0,小时返回23。
这些旅行中有11,000次,我想要得到的是每周根据时间驾驶的汽车数量。
这可以通过计算在指定时间间隔内各自['BeginTime']和['EndTime']间隔之间的跳闸次数来推断。数据距离最近的五分钟就足够了五分钟。
有一种优雅的蟒蛇方式吗?类似的东西:
for fiveMinutes in Week:
count = 0
for trip in range(len(journey['BeginTime']):
if journey['BeginTime'][trip] == fiveMinutes
or (journey['BeginTime'][trip] < fiveMinutes
and journey['EndTime'][trip] > fiveMinutes):
count = count + 1
carCount[fiveMinutes] = count
答案 0 :(得分:0)
如果这有帮助,这是一个想法......
from datetime import datetime, timedelta
# This does not check for crossing from Sunday to Monday
def convert_dt(start_dt, journey):
begin_weekday, begin_hour, begin_minute = journey[0]
end_weekday, end_hour, end_minute = journey[1]
begin_dt = start_dt + timedelta(days=begin_weekday)
begin_dt += timedelta(hours=begin_hour, minutes=begin_minute)
end_dt = start_dt + timedelta(days=end_weekday)
end_dt += timedelta(hours=end_hour,minutes=end_minute)
return (begin_dt, end_dt)
def get_slot_journeys(start_dt, journeys):
next_dt = start_dt
slot_count = 60/5 * 24 * 7
slot_dict = {}
journey_dts = []
#convert journey begin and end to datetimes
for index in range(len(journeys['begin_weekday'])):
next_journey = [(journeys['begin_weekday'][index],
journeys['begin_hour'][index],
journeys['begin_minute'][index],),
(journeys['end_weekday'][index],
journeys['end_hour'][index],
journeys['end_minute'][index],)
]
journey_dts.append(convert_dt(start_dt, next_journey))
for slot in range(slot_count):
slot_dict[next_dt] = 0
for journey_start, journey_end in journey_dts:
if next_dt >= journey_start and next_dt <= journey_end:
slot_dict[next_dt] = slot_dict[next_dt] + 1
next_dt += timedelta(minutes=(5))
return slot_dict
if __name__ == "__main__":
start_dt = datetime(2012, 1, 2, 0, 0)
journeys = {'begin_weekday': [0, 0],
'begin_hour': [14, 18],
'begin_minute': [20, 30],
'end_weekday': [0, 1],
'end_hour': [19, 12],
'end_minute': [15, 55],
}
slot_dict = get_slot_journeys(start_dt, journeys)
slot_keys = slot_dict.keys()
slot_keys.sort()
for key in slot_keys:
if slot_dict[key]:
print key, slot_dict[key]