Skip to content Skip to sidebar Skip to footer

In Dataframe: How To Pull Minutes And Seconds Combinedly(mm:ss) From Timedelta Using Python

i have already tried these but by this we can only get hour/minute/second but not both minute and second In[7]: df['B'] = df['A'].dt.components['hours'] df Out[7]: A B 0 02:

Solution 1:

You can convert the timedelta to total seconds and use divmod function to extract minutes and seconds from the total seconds.

Edit: Removed lambda function from the first operation.

    df['sec'] = df['A'].dt.total_seconds().astype(int)
    df['B'] = df.apply(lambda row: str(divmod(divmod(row['sec'], 3600)[1], 60)[0]).zfill(2)+':'+str(divmod(divmod(row['sec'], 3600)[1], 60)[1]).zfill(2), axis=1)

Post a Comment for "In Dataframe: How To Pull Minutes And Seconds Combinedly(mm:ss) From Timedelta Using Python"