Lynn
Lynn

Reputation: 4388

Combine two date columns together to one in Python

I have a dataframe, df, where I would like to combine a start and end column into one single date column.

start             end              id

10/01/2020        11/01/2020       a

Desired output

Date id

10/01/2020 to 11/01/2020           a

OR

 Date                            id

10/01/2020  11/01/2020            a

This is what I am doing

df1 = df['Date'] = df['start'] + "To " + df['end']


df11.info()
1   start  188 non-null    datetime64[ns]
2   end    188 non-null    datetime64[ns]

I am still researching this, I think I may need to do a conversion to Datetime, however, I see that both of the columns I wish to combine already has a datetime type. Any suggestion is appreciated.

Upvotes: 0

Views: 286

Answers (1)

wwnde
wwnde

Reputation: 26676

df['Date']= df.apply(lambda x: x['start'] + ' '+'to'+ ' '+x['end'],1)

Or

df['Date']=df.groupby(df.index).apply(lambda x:x['start'].str.cat(x['end'], sep=' to '))



 start         end id                      Date
0  10/01/2020  11/01/2020  a  10/01/2020 to 11/01/2020

Upvotes: 1

Related Questions