簡體   English   中英

從其他年月日列中創建 python 中的 datetime.date 列

[英]Make datetime.date column in python from other year,month and day column

我試圖通過組合年、月和日列來制作一個 datetime.date 類型的列。

這是我嘗試過的。

        df['date']= date(df['year'],df['month'],df['day'])

它給了我一個錯誤:

TypeError: cannot convert the series to <class 'int'>

我嘗試使用 int() 進行解析,但它給了我同樣的錯誤。

您可以將其轉換為字符串日期時間,然后使用 to_datetime 獲取您想要的系列。 將 x[1] 視為月,將 x[0] 視為日,將 x[2] 視為年。

df = pd.DataFrame([[2,1,2000],[2,3,2003]])
df['new'] = pd.to_datetime(df.apply(lambda x: f"{x[1]}-{x[0]}-{x[2]}", axis=1))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM