Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine multiple columns in a Data Frame to Pandas datetime format

I have a pandas data frame with values as below

ProcessID1 UserID Date Month Year Time 248 Tony 29 4 2017 23:30:56 436 Jeff 28 4 2017 20:02:19 500 Greg 4 5 2017 11:48:29 I would like to know is there any way I can combine columns of Date,Month&Year & time to a pd.datetimeformat?

like image 583
Jithesh Erancheri Avatar asked Apr 08 '18 14:04

Jithesh Erancheri


People also ask

How do I merge datetime columns in pandas?

Pandas Combine() Function combine() function which allows us to take a date and time string values and combine them to a single Pandas timestamp object. The function accepts two main parameters: Date – refers to the datetime. date object denoting the date string.

How do I merge multiple columns in pandas?

To merge two pandas DataFrames on multiple columns use pandas. merge() method. merge() is considered more versatile and flexible and we also have the same method in DataFrame.

Which function is used for assembling a datetime from multiple columns of a DataFrame?

to_datetime() can be used to assemble a datetime from multiple columns as well. The keys (columns label) can be common abbreviations like ['year', 'month', 'day', 'minute', 'second', 'ms', 'us', 'ns']) or plurals of the same.

How do I append multiple columns to a data frame?

Using DataFrame. insert() method, we can add new columns at specific position of the column name sequence. Although insert takes single column name, value as input, but we can use it repeatedly to add multiple columns to the DataFrame.


1 Answers

Use to_datetime with automatic convert column Day,Month,Year with add times converted to_timedelta:

df['Datetime'] = pd.to_datetime(df.rename(columns={'Date':'Day'})[['Day','Month','Year']]) + \
                 pd.to_timedelta(df['Time'])

Another solutions are join all column converted to strings first:

df['Datetime'] = pd.to_datetime(df[['Date','Month','Year', 'Time']]
                   .astype(str).apply(' '.join, 1), format='%d %m %Y %H:%M:%S')
df['Datetime']  = (pd.to_datetime(df['Year'].astype(str) + '-' +
                                  df['Month'].astype(str) + '-' +
                                  df['Date'].astype(str) + ' ' +
                                  df['Time']))

print (df)
   ProcessID1 UserID  Date  Month  Year      Time            Datetime
0         248   Tony    29      4  2017  23:30:56 2017-04-29 23:30:56
1         436   Jeff    28      4  2017  20:02:19 2017-04-28 20:02:19
2         500   Greg     4      5  2017  11:48:29 2017-05-04 11:48:29

Last if need remove these columns:

df = df.drop(['Date','Month','Year', 'Time'], axis=1)
print (df)
   ProcessID1 UserID            Datetime
0         248   Tony 2017-04-29 23:30:56
1         436   Jeff 2017-04-28 20:02:19
2         500   Greg 2017-05-04 11:48:29
like image 59
jezrael Avatar answered Oct 05 '22 14:10

jezrael