Reputation: 43
I have tried searching other posts on here, but can't seem to solve this problem. I have a CSV file in which Year, Crash_Month, Crash_Day and Crash_Time are all seperate columns in the CSV 'data_dict'. I am trying to solve the below question. How would I go about this? I have tried to use a data frame, and pandas convert to datetime, but I'm not sure if this is the right approach. Many thanks
Here's the data frame I'm trying to assign datetime to
year month day time
0 2000 1 1 4:30:59
1 2000 1 1 0:07:35
2 2000 1 1 4:51:37
3 2000 1 1 4:27:56
4 2000 1 1 2:16:31
5 2000 1 1 0:37:21
6 2000 1 1 0:52:57
7 2000 1 1 3:35:14
8 2000 1 1 2:41:58
9 2000 1 1 3:43:02
10 2000 1 1 3:49:19
11 2000 1 1 3:03:55
12 2000 1 1 4:46:01
13 2000 1 1 1:07:24
14 2000 1 1 8:29:04
15 2000 1 1 6:35:21
16 2000 1 1 6:06:25
17 2000 1 1 7:10:13
18 2000 1 1 10:57:24
19 2000 1 1 7:54:38
So far, I have coded this.
import pandas as pd
df = pd.DataFrame({'year': (data_dict['Year']),
'month': (data_dict['Crash_Month']),
'day': (data_dict['Crash_Day']),
'time': (data_dict['Crash_Time'])})
date=pd.to_datetime(df[["year", "month", "day", "time"]],format='%YYYY%mm%dd, %HH%MM%SS')
print(date)
day_of_week = {0 : 'Monday',
1: 'Tuesday',
2: 'Wednesday',
3: 'Thursday',
4: 'Friday',
5: 'Saturday',
6: 'Sunday'}
month_season= {1: 'Summer',
2: 'Summer',
3: 'Autumn',
4: 'Autumn',
5: 'Autumn',
6: 'Winter',
7: 'Winter',
8: 'Winter',
9: 'Spring',
10: 'Spring',
11: 'Spring',
12: 'Summer'}
Upvotes: 0
Views: 178
Reputation: 23099
We can use str.zfill
and string concenation with pd.to_datetime
to build up your datetime.
df2['date'] = pd.to_datetime(df2['year'].astype(str)
+ df2['month'].astype(str).str.zfill(2)
+ df2['day'].astype(str).str.zfill(2)
+ ' '
+ df2['time'].astype(str),format='%Y%m%d %H:%M:%S'
)
year month day time date
0 2000 1 1 4:30:59 2000-01-01 04:30:59
1 2000 1 1 0:07:35 2000-01-01 00:07:35
2 2000 1 1 4:51:37 2000-01-01 04:51:37
3 2000 1 1 4:27:56 2000-01-01 04:27:56
4 2000 1 1 2:16:31 2000-01-01 02:16:31
5 2000 1 1 0:37:21 2000-01-01 00:37:21
6 2000 1 1 0:52:57 2000-01-01 00:52:57
7 2000 1 1 3:35:14 2000-01-01 03:35:14
8 2000 1 1 2:41:58 2000-01-01 02:41:58
9 2000 1 1 3:43:02 2000-01-01 03:43:02
10 2000 1 1 3:49:19 2000-01-01 03:49:19
11 2000 1 1 3:03:55 2000-01-01 03:03:55
12 2000 1 1 4:46:01 2000-01-01 04:46:01
13 2000 1 1 1:07:24 2000-01-01 01:07:24
14 2000 1 1 8:29:04 2000-01-01 08:29:04
15 2000 1 1 6:35:21 2000-01-01 06:35:21
16 2000 1 1 6:06:25 2000-01-01 06:06:25
17 2000 1 1 7:10:13 2000-01-01 07:10:13
18 2000 1 1 10:57:24 2000-01-01 10:57:24
19 2000 1 1 7:54:38 2000-01-01 07:54:38
Upvotes: 1