user7400738 user7400738 - 4 years ago 206
Python Question

how to make a new column by joining to existing column in pandas

I know there are a lot of similar type of this question but I can't find my way around through existing answers. It's a simple question but still I am stuck on this small problem. I have a data with lots of columns. I want to create a new column using two of the existing columns. Something like this:

Index Date Time
0 26-10-2017 06:00:00
1 26-10-2017 06:15:00
2 26-10-2017 06:30:00


Now I need a new column which should look like this:

Index Date Time Datetime
0 26-10-2017 06:00:00 26-10-2017 06:00:00
1 26-10-2017 06:15:00 26-10-2017 06:15:00
2 26-10-2017 06:30:00 26-10-2017 06:30:00


I am unable to do that exactly right now. I am currently getting a tuple with two string values but I don't want this:

Index Date Time Datetime
0 26-10-2017 06:00:00 ('26-10-2017', '06:00:00')
1 26-10-2017 06:15:00 ('26-10-2017', '06:15:00')
2 26-10-2017 06:30:00 ('26-10-2017', '06:30:00')


I am using the following code:

df5['Datetime'] = list(zip(df5.Date, df5.Time))


Is there anyway I can just get the value the way I want not in tuples?

Answer Source

assuming columns are strings, add them up and use pd.to_datetime

df.assign(DateTime=pd.to_datetime(df.Date + ' ' + df.Time))

   Index        Date      Time            DateTime
0      0  26-10-2017  06:00:00 2017-10-26 06:00:00
1      1  26-10-2017  06:15:00 2017-10-26 06:15:00
2      2  26-10-2017  06:30:00 2017-10-26 06:30:00
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download