-3

I have written this code:

df.loc[df['Profile Name'] == 'karladdo201', 'Duration'].astype('timedelta64[s]').sum()

and get this value:

Timedelta('8 days 12:02:45')

How do I get a values similar or in this format ?: 4138000.0

from writing this code:

viewTime = {}
viewTime.update({"karladdo201": df.loc[df['Profile Name']=='karladdo201','Duration'].astype('timedelta64[s]').sum()})
viewTime

They all come from a dataframe of my Netflix viewing history. I am using jupyter lab

1
  • 1
    Please read How do I ask a good question? and use it as a guide to writing your post. In particular, pay attention to the third point of 'Write a title that summarizes the specific problem'. I added bold to emphasize 'specific'. The current title of 'How do I Convert data type in Python' can be answered in ways that wouldn't help you much with your specific issue. One thing to imagine is that you may encounter this same issue a few months from now and you'd want to be able to see the specific one you are looking for from a list. Commented Jul 7 at 19:58

1 Answer 1

1

To convert a Timedelta into a float (the total number of seconds), you likely want the total number of seconds represented by that time duration.

To represent the Timedelta in total number of seconds time duration you could use:

import pandas as pd
td = pd.Timedelta('8 days 12:02:45')
total_seconds = td.total_seconds()
print(total_seconds)

Since you are using pandas as far as i understand from your question tags.

If needed you can get the result in milliseconds, instead of seconds like so:


milliseconds = td.total_seconds() * 1000
print(milliseconds)

P.S. Im not sure what you mean by:

4138000.0

I cannot connect it with the days you've mentioned in your question.

Sign up to request clarification or add additional context in comments.

1 Comment

Converting to seconds worked. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.