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

后端 未结 5 1629
借酒劲吻你
借酒劲吻你 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:40

    We can simply use the pandas apply() function

    def get_seconds(time_delta):
        return time_delta.seconds
    
    def get_microseconds(time_delta):
        return time_delta.micro_seconds
    
    time_delta_series = df['duration']
    
    converted_series = time_delta_series.apply(get_seconds)
    print(converted_series)
    

提交回复
热议问题