Python Pandas - 從 TimeDelta 物件中獲取持續時間的總秒數
如需從 TimeDelta 物件中獲取持續時間的總秒數,請使用 timedelta.total_seconds() 方法。
首先,匯入所需的庫 −
import pandas as pd
TimeDeltas 是 Python 的標準 datetime 庫,使用 timedelta 的不同表示法,建立一個 TimeDelta 物件 −
timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns')
顯示 TimeDelta −
print("Timedelta...\n", timedelta)獲取總秒數 −
timedelta.total_seconds()
示例
以下是程式碼 −
import pandas as pd
# TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s
# create a Timedelta object
timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns')
# display the Timedelta
print("Timedelta...\n", timedelta)
# get the total seconds
res = timedelta.total_seconds()
# Return the result i.e. the total seconds in the duration
print("\nTotal seconds...\n", res)輸出
將生成以下程式碼 −
Timedelta... 2 days 11:22:25.050000045 Total seconds... 213745.05
廣告
資料結構
網路
資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP