In dataframe: how to pull minutes and seconds combinedly(mm:ss) from timedelta using python

2024/10/6 20:37:26

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:00:00 2

1 01:00:00 1


from this timedelta format

0 0 days 00:02:02.246000

1 0 days 00:01:59.127000

i need only minutes and seconds, how to get it as below

02:02

01:59

Answer

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)
https://en.xdnf.cn/q/118914.html

Related Q&A

Understanding python numpy syntax to mask and filter array

Please help me understand how the lines below are working.How does a pair of parentheses create an array and then individual elements go through logical condition check and create a new array? How doe…

How to get the proper link from a website using python beautifulsoup?

When I try to scrape roster links, I get https://gwsports.com/roster.aspx?path=wpolo when I open it on chrome it changes to https://gwsports.com/sports/mens-water-polo/roster. I want to scrape it in p…

tkinter frame propagate not behaving?

If you uncomment the options_frame_title you will see that it does not behave properly. Am I missing something? That section was just copied and pasted from the preview_frame_title and that seems to h…

python modules installing Error Visual c++ 14.0 is required [duplicate]

This question already has answers here:pip install ecos errors with "Microsoft Visual C++ 14.0 is required." [duplicate](1 answer)Error "Microsoft Visual C++ 14.0 is required (Unable to …

How to render Flask Web App with Javascript [duplicate]

This question already has answers here:Return JSON response from Flask view(15 answers)How to append ajax html response to next to current div(5 answers)Closed 5 years ago.Edit: Hi Ive checked the dupl…

Use start and stop function with same button in Tkinter

With the help of the command button, I am able to disconnect the frame in Tkinter. But is there any way which helps to use the same button to start also?import tkinter as tk counter = 0 def counter_la…

How to restrict my students to dont access teacher area in django? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

invert edge values in python boolean list

I have a list of booleans likel = [False, False, False, True, True, True, False, False, True, False, False]and want to invert every edge value that is False like[True, True, True, True, True, True, Fal…

Unable to write text on mouseclick area on Image

I am trying to draw text on Image where the user clicks. Getting this error:Exception in Tkinter callback Traceback (most recent call last):File "C:\Users\Admin\AppData\Local\Programs\Python\Pytho…

Google Cloud Storage: __init__() got an unexpected keyword argument total_size

I am developping a tool to transcribe interviews for a contract I have. For that I develop a code with the following flow:After input validation, the audio file (in m4a) is converted to wav and stored …