2017-06-19 53 views
1

我有數據在Excel這樣結合的時間和日期單獨列一個在熊貓

enter image description here

我想用下面的代碼

import pandas 
df = pd.read_excel('selfmade.xlsx') 

df['new'] = df['Date'].map(str) + df['Time'].map(str) 
print(df) 

日期和時間的列組合在一起但它打印這樣的結果。

enter image description here

我想在格式像2016-06-14 10:00:00

最後一欄,我應該在我的代碼,以獲得期望的結果

回答

1

我想你需要to_datetimeto_timedelta改變,也需要轉換Time column to stringastype

df['new'] = pd.to_datetime(df['Date']) + pd.to_timedelta(df['Time'].astype(str)) 

如果Datedtype已經datetime

df['new'] = df['Date'] + pd.to_timedelta(df['Time'].astype(str)) 
+0

不工作,'類型錯誤:類型的對象datetime.time'沒有LEN()' –

+0

也許是necesary轉換爲'str',檢查編輯答案。 – jezrael

+1

感謝兄弟。它的工作現在 –