問題描述
我有以下數據框df
:
id lat lon year month day
0 381 53.30660 -0.54649 2004 1 2
1 381 53.30660 -0.54649 2004 1 3
2 381 53.30660 -0.54649 2004 1 4
我想創建一個新列 df['Date']
,其中 year
、month
和 day
列按 yyyy-md
格式組合.
and I want to create a new column df['Date']
where the year
, month
, and day
columns are combined according to the format yyyy-m-d
.
在這篇文章之后,我做到了:
`df['Date']=pd.to_datetime(df['year']*10000000000
+df['month']*100000000
+df['day']*1000000,
format='%Y-%m-%d%')`
結果不是我預期的,因為它是從 1970 年而不是 2004 年開始的,而且它還包含我沒有指定的小時戳:
The result is not what I expected, as it starts from 1970 instead of 2004, and it also contains the hour stamp, which I did not specify:
id lat lon year month day Date
0 381 53.30660 -0.54649 2004 1 2 1970-01-01 05:34:00.102
1 381 53.30660 -0.54649 2004 1 3 1970-01-01 05:34:00.103
2 381 53.30660 -0.54649 2004 1 4 1970-01-01 05:34:00.104
由于日期應該是 2004-1-2
格式,我做錯了什么?
As the dates should be in the 2004-1-2
format, what am I doing wrong?
推薦答案
有一個更簡單的方法:
In [250]: df['Date']=pd.to_datetime(df[['year','month','day']])
In [251]: df
Out[251]:
id lat lon year month day Date
0 381 53.3066 -0.54649 2004 1 2 2004-01-02
1 381 53.3066 -0.54649 2004 1 3 2004-01-03
2 381 53.3066 -0.54649 2004 1 4 2004-01-04
來自 文檔:
從 DataFrame 的多列中組裝日期時間.按鍵可以是常見的縮寫,如 [year
、month
、day
、minute
、second
、ms
、us
、ns
])或相同的復數形式
Assembling a datetime from multiple columns of a DataFrame. The keys can be common abbreviations like [
year
,month
,day
,minute
,second
,ms
,us
,ns
]) or plurals of the same
這篇關于如何將年、月和日列合并到單個日期時間列?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!