Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- start = [pd.to_datetime('03:01'),pd.to_datetime('03:08'),pd.to_datetime('03:06')]
- finish = [pd.to_datetime('03:11'),pd.to_datetime('03:13'),pd.to_datetime('03:16')]
- df1 = pd.DataFrame()
- df1['passengers'] = [10, 15, 20]
- df1['t1'] = start
- df1['t2'] = finish
- df1
- passengers t1 t2
- 0 10 2019-07-20 03:01:00 2019-07-20 03:11:00
- 1 15 2019-07-20 03:08:00 2019-07-20 03:13:00
- 2 20 2019-07-20 03:06:00 2019-07-20 03:16:00
- cuthour = '03:00' # <--- user input
- cuthour = pd.to_datetime(cuthour)
- idx = pd.date_range(cuthour+pd.to_timedelta('1min'),'23:59', freq='T').append(pd.date_range('00:00',cuthour,freq='T'))
- df2 = pd.DataFrame(index=idx)
- df2['passengers'] = 0 #initialize with zeros
- <class 'pandas.core.frame.DataFrame'>
- DatetimeIndex: 1440 entries, 2019-07-20 03:01:00 to 2019-07-20 03:00:00
- Data columns (total 1 columns):
- pessoas 1440 non-null int64
- dtypes: int64(1)
- 2019-07-20 03:01:00 1.0
- 2019-07-20 03:02:00 1.0
- 2019-07-20 03:03:00 1.0
- 2019-07-20 03:04:00 1.0
- 2019-07-20 03:05:00 1.0
- 2019-07-20 03:06:00 1.5
- 2019-07-20 03:07:00 1.5
- 2019-07-20 03:08:00 2.0
- 2019-07-20 03:09:00 2.0
- 2019-07-20 03:10:00 2.0
- 2019-07-20 03:11:00 2.0
- 2019-07-20 03:12:00 2.5
- 2019-07-20 03:13:00 2.0
- 2019-07-20 03:14:00 2.0
- 2019-07-20 03:15:00 2.0
- 2019-07-20 03:16:00 0.0
- 2019-07-20 03:17:00 0.0
- ...
- 2019-07-20 03:01:00 1.0
- 2019-07-20 03:02:00 1.0
- 2019-07-20 03:03:00 1.0
- 2019-07-20 03:04:00 1.0
- 2019-07-20 03:05:00 1.0
- 2019-07-20 03:06:00 3.0
- 2019-07-20 03:07:00 3.0
- 2019-07-20 03:08:00 6.0
- 2019-07-20 03:09:00 6.0
- 2019-07-20 03:10:00 6.0
- 2019-07-20 03:11:00 6.0
- 2019-07-20 03:12:00 5.0
- 2019-07-20 03:13:00 5.0
- 2019-07-20 03:14:00 2.0
- 2019-07-20 03:15:00 2.0
- 2019-07-20 03:16:00 2.0
- 2019-07-20 03:17:00 0.0
- ...
- def calcmean(x):
- ir = pd.date_range(x.t1,x.t2,freq='T')
- lir = len(ir)-1
- mp = x.passengers/lir
- df2.loc[df2.index.isin(ir)] = df2.loc[df2.index.isin(ir)] + mp
- df1.apply(calcmean, axis=1)
- df2
- mean of... TOTAL total
- HH:MM trip1 trip2 trip3 MEAN sum
- 03:01 1 1 1
- 03:02 1 1 1
- 03:03 1 1 1
- 03:04 1 1 1
- 03:05 1 1 1
- 03:06 1 2 1.5 3
- 03:07 1 2 1.5 3
- 03:08 1 3 2 2 6
- 03:09 1 3 2 2 6
- 03:10 1 3 2 2 6
- 03:11 3 2 2 6
- 03:12 3 2 2.5 5
- 03:13 2 2 2
- 03:14 2 2 2
- 03:15 2 2 2
- Mean trip1 = 10 / 10 minutes = 1 passenger/minute
- Mean trip2 = 15 / 5 minutes = 3 passengers/minute
- Mean trip3 = 20 / 10 minutes = 2 passengers/minute
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement