Dreamer Dreamer - 4 months ago 26
Python Question

Break values of one column into two columns

I have a data frame with a column "last_updated", with type datetime64[ns]:

df = pd.DataFrame({'last_updated': ['11/12/14 2:44 PM','5/18/15 11:36 AM','11/12/14
3:09 PM']})

I want to create two columns out of this one single column - "last_updated_date" and "last_updated_time". Also, time should be 24-hour format.

How can I accomplish this task using pandas?


You could cast the datetime column into strings (date and time parts) after converting them into 24 hour format as shown:

new_cols = ['last_updated_date', 'last_updated_time']
df[new_cols] = pd.to_datetime(df['last_updated'], format='%m/%d/%y %I:%M %p')   \



This would retain the values as it is while exporting it to excel.