How To Add Extra Time Into The Time Column With Data Using Python
Here I have a dataset with date, time and one input column. So here my time column is not good. So I want to give time range into that time column. So here first I did I just conve
Solution 1:
Use:
data['date']= pd.to_datetime(data['date'] + " " + data['time'],
format='%d/%m/%Y %H:%M:%S', dayfirst=True)
Subtract firat value of date column and convert to minutes:
data['time1'] = data['date'].sub(data.loc[0, 'date']).dt.total_seconds()/60
print (data)
date time X3 time1
0 2018-03-10 06:15:00 6:15:00 7 0.0
1 2018-03-10 06:45:00 6:45:00 5 30.0
2 2018-03-10 07:45:00 7:45:00 7 90.0
3 2018-03-10 09:00:00 9:00:00 7 165.0
4 2018-03-10 09:25:00 9:25:00 7 190.0
5 2018-03-10 09:30:00 9:30:00 5 195.0
6 2018-03-10 11:00:00 11:00:00 7 285.0
7 2018-03-10 11:30:00 11:30:00 7 315.0
8 2018-03-10 13:30:00 13:30:00 7 435.0
9 2018-03-10 13:50:00 13:50:00 5 455.0
10 2018-03-10 15:00:00 15:00:00 7 525.0
11 2018-03-10 15:25:00 15:25:00 7 550.0
12 2018-03-10 16:25:00 16:25:00 7 610.0
13 2018-03-10 18:00:00 18:00:00 7 705.0
14 2018-03-10 19:00:00 19:00:00 5 765.0
Create new 60
range values:
arr = np.arange(0, int(data['time1'].max()), 60)
print (arr)
[ 0 60 120 180 240 300 360 420 480 540 600 660 720]
Join together with minute column:
union = np.union1d(data['time1'], arr)
print (union)
[ 0. 30. 60. 90. 120. 165. 180. 190. 195. 240. 285. 300. 315. 360.
420. 435. 455. 480. 525. 540. 550. 600. 610. 660. 705. 720. 765.]
Create index by time1
column and DataFrame.reindex
- added new values from arr
:
data = data.set_index('time1')['X3'].reindex(union, fill_value=0).reset_index()
print (data)
time1 X3
0 0.0 7
1 30.0 5
2 60.0 0
3 90.0 7
4 120.0 0
5 165.0 7
6 180.0 0
7 190.0 7
8 195.0 5
9 240.0 0
10 285.0 7
11 300.0 0
12 315.0 7
13 360.0 0
14 420.0 0
15 435.0 7
16 455.0 5
17 480.0 0
18 525.0 7
19 540.0 0
20 550.0 7
21 600.0 0
22 610.0 7
23 660.0 0
24 705.0 7
25 720.0 0
26 765.0 5
Post a Comment for "How To Add Extra Time Into The Time Column With Data Using Python"