簡體   English   中英

當小時/分鍾/秒是 python 或 pandas 中的單個數字時,有誰知道如何正確格式化時間

[英]Does anyone know how to get the time to format correctly when the Hour/Minute/Second is a SINGLE digit in python or pandas

我的 dataframe 中的某些行的時間為“13:2:7”,我希望它們為“13:02:07”。

我嘗試將 pd.to_datetime 應用於列,但它不起作用

有人可以建議一些方法以標准格式格式化時間

找到了解決方案

我通過 pandas to_timedelta解決了

pd.to_timedelta("13:2:3")或通過它傳遞 dataframe 的列

結果:

“13:02:03”

ps:你會得到“0天13:02:03”的結果

為了刪除 0 天

df["column_name"].astype(str).map(lambda x: x[7:]))

我們將對存在的初始 0 天字符串進行切片

注意:最終結果將在表單字符串中

如果您想要時間 object,

使用來自datetime模塊的 pandas pd.to_datetimestrftime

暫無
暫無

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

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