Convert timedelta64[ns] column to seconds in Python Pandas DataFrame

后端 未结 5 1630
借酒劲吻你
借酒劲吻你 2020-11-28 06:54

A pandas DataFrame column duration contains timedelta64[ns] as shown. How can you convert them to seconds?

0   00:20:32
1   00:23:1         


        
5条回答
  •  误落风尘
    2020-11-28 07:30

    Use the Series dt accessor to get access to the methods and attributes of a datetime (timedelta) series.

    >>> s
    0   -1 days +23:45:14.304000
    1   -1 days +23:46:57.132000
    2   -1 days +23:49:25.913000
    3   -1 days +23:59:48.913000
    4            00:00:00.820000
    dtype: timedelta64[ns]
    >>>
    >>> s.dt.total_seconds()
    0   -885.696
    1   -782.868
    2   -634.087
    3    -11.087
    4      0.820
    dtype: float64
    

    There are other Pandas Series Accessors for String, Categorical, and Sparse data types.

提交回复
热议问题