converting a muti-time formatted string into seconds (pandas)

mkmkmk123

I've been searching for a solution to this and the closest I got was here: Convert time string expressed as <number>[m|h|d|s|w] to seconds in Python

however none of the solutions work because the time format only sometimes contains one unit and is inconsistant throughout the column. e.g.

['4h 30m 24s', '13w 5d', '11w']

when I .apply() this over the entire column it fails. How can I convert all of these rows into seconds? I tried df['time_value'].str.split() but this is a very messy and seemingly inefficient way to do this, there must be a better way?

eumiro

How about applying this method?

def convert_to_seconds(s):
    seconds = 0
    seconds_per_unit = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800}
    for part in s.split():
        number = int(part[:-1])
        unit = part[-1]
        seconds += number * seconds_per_unit[unit]
    return seconds

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Convert time into seconds string issue

分類Dev

extracting clock seconds from yearmonth formatted string in Tcl

分類Dev

How to get local time as a formatted string in Lua

分類Dev

Converting a hex to string in Swift formatted to keep the same number of digits

分類Dev

Converting time in java with string manipulation

分類Dev

Signed time deltas to signed seconds in Pandas

分類Dev

Converting timestamp column in custom string format to seconds in python

分類Dev

Pandas - Converting column of type float to date time

分類Dev

Converting a time String to ISO 8601 format

分類Dev

Converting String to Time without Date in Java

分類Dev

Converting a time string to append with DateTime object

分類Dev

Python display milliseconds in formatted string using `time.strftime`

分類Dev

How to get a formatted date and time string from `now`?

分類Dev

Converting string date to epoch time not working with Cython and POSIX C libraries

分類Dev

Issue converting Boolean to String (works some of the time but not all)

分類Dev

How to remove time segment completely from datetime after converting to string?

分類Dev

conversion failed when converting date and/or time from character string in xml

分類Dev

Converting a string of numbers to hex and back to dec pandas python

分類Dev

Python: Converting a seconds to a datetime format in a dataframe column

分類Dev

Regex to select seconds from time string (all times not in the same format) without lookbehind

分類Dev

Converting a String to Set<String>

分類Dev

Append formatted string to already formatted string from UITextView?

分類Dev

Converting a timestamp to relative time

分類Dev

Converting strings to time

分類Dev

Converting string to integer and then converting back to string

分類Dev

Conversion failed when converting date and/or time from character string - but no idea why

分類Dev

Converting date and time from String format to Python datetime object: ValueError: time data '... p.m.' does not match format '... %p'

分類Dev

Add trailing zero to awk formatted time output

分類Dev

extracting an integer from aKorean text string, conditional on its content and converting it to float in pandas

Related 関連記事

  1. 1

    Convert time into seconds string issue

  2. 2

    extracting clock seconds from yearmonth formatted string in Tcl

  3. 3

    How to get local time as a formatted string in Lua

  4. 4

    Converting a hex to string in Swift formatted to keep the same number of digits

  5. 5

    Converting time in java with string manipulation

  6. 6

    Signed time deltas to signed seconds in Pandas

  7. 7

    Converting timestamp column in custom string format to seconds in python

  8. 8

    Pandas - Converting column of type float to date time

  9. 9

    Converting a time String to ISO 8601 format

  10. 10

    Converting String to Time without Date in Java

  11. 11

    Converting a time string to append with DateTime object

  12. 12

    Python display milliseconds in formatted string using `time.strftime`

  13. 13

    How to get a formatted date and time string from `now`?

  14. 14

    Converting string date to epoch time not working with Cython and POSIX C libraries

  15. 15

    Issue converting Boolean to String (works some of the time but not all)

  16. 16

    How to remove time segment completely from datetime after converting to string?

  17. 17

    conversion failed when converting date and/or time from character string in xml

  18. 18

    Converting a string of numbers to hex and back to dec pandas python

  19. 19

    Python: Converting a seconds to a datetime format in a dataframe column

  20. 20

    Regex to select seconds from time string (all times not in the same format) without lookbehind

  21. 21

    Converting a String to Set<String>

  22. 22

    Append formatted string to already formatted string from UITextView?

  23. 23

    Converting a timestamp to relative time

  24. 24

    Converting strings to time

  25. 25

    Converting string to integer and then converting back to string

  26. 26

    Conversion failed when converting date and/or time from character string - but no idea why

  27. 27

    Converting date and time from String format to Python datetime object: ValueError: time data '... p.m.' does not match format '... %p'

  28. 28

    Add trailing zero to awk formatted time output

  29. 29

    extracting an integer from aKorean text string, conditional on its content and converting it to float in pandas

ホットタグ

アーカイブ