Calculate Time in pandas by extracting












0















I have a set of data



Time1  Time2
XY40M XY35M
XY5H XY45M
XY30M XY20M
XY1H XY2H
XY1H30M XY2H


I have to calculate the Total time in minutes



Time1+Time2
75
345
50
180
210


How can i derive this?










share|improve this question





























    0















    I have a set of data



    Time1  Time2
    XY40M XY35M
    XY5H XY45M
    XY30M XY20M
    XY1H XY2H
    XY1H30M XY2H


    I have to calculate the Total time in minutes



    Time1+Time2
    75
    345
    50
    180
    210


    How can i derive this?










    share|improve this question



























      0












      0








      0








      I have a set of data



      Time1  Time2
      XY40M XY35M
      XY5H XY45M
      XY30M XY20M
      XY1H XY2H
      XY1H30M XY2H


      I have to calculate the Total time in minutes



      Time1+Time2
      75
      345
      50
      180
      210


      How can i derive this?










      share|improve this question
















      I have a set of data



      Time1  Time2
      XY40M XY35M
      XY5H XY45M
      XY30M XY20M
      XY1H XY2H
      XY1H30M XY2H


      I have to calculate the Total time in minutes



      Time1+Time2
      75
      345
      50
      180
      210


      How can i derive this?







      python pandas






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 12:50









      user10465355

      1,9432416




      1,9432416










      asked Mar 11 '18 at 5:46









      Tom J MuthirenthiTom J Muthirenthi

      1,14411328




      1,14411328
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Use str.extract with numpy.where:



          a = df['Time1'].str.extract('(d+[MH])', expand=False)
          a1 = a.str[:-1].astype(int)
          b = df['Time2'].str.extract('(d+[MH])', expand=False)
          b1 = b.str[:-1].astype(int)

          df['Time'] = np.where(a.str[-1] == 'H', a1 * 60, a1) + np.where(b.str[-1] == 'H', b1 * 60, b1)


          Another solution:



          a = df['Time1'].str.extract('(d+)([MH])', expand=True)
          a1 = a[0].astype(int)
          b = df['Time2'].str.extract('(d+)([MH])', expand=True)
          b1 = b[0].astype(int)

          df['Time'] = np.where(a[1] == 'H', a1 * 60, a1) + np.where(b[1] == 'H', b1 * 60, b1)




          print (df)
          Time1 Time2 Time
          0 XY40M XY35M 75
          1 XY5H XY45M 345
          2 XY30M XY20M 50
          3 XY1H XY2H 180


          EDIT:



          a = df['Time1'].str.extract('(d+)([MH])(d*)([M]*)', expand=True)
          a1 = a[[0,2]].replace('', 0).astype(int)
          b = df['Time2'].str.extract('(d+)([MH])(d*)([M]*)', expand=True)
          b1 = b[[0,2]].replace('', 0).astype(int)

          df['Time'] = np.where(a[1] == 'H', a1[0] * 60, a1[0]) + a1[2] +
          np.where(b[1] == 'H', b1[0] * 60, b1[0]) + b1[2]

          print (df)
          Time1 Time2 Time
          0 XY40M XY35M 75
          1 XY5H XY45M 345
          2 XY30M XY20M 50
          3 XY1H XY2H 180
          4 XY1H30M XY2H 210





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49217070%2fcalculate-time-in-pandas-by-extracting%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            Use str.extract with numpy.where:



            a = df['Time1'].str.extract('(d+[MH])', expand=False)
            a1 = a.str[:-1].astype(int)
            b = df['Time2'].str.extract('(d+[MH])', expand=False)
            b1 = b.str[:-1].astype(int)

            df['Time'] = np.where(a.str[-1] == 'H', a1 * 60, a1) + np.where(b.str[-1] == 'H', b1 * 60, b1)


            Another solution:



            a = df['Time1'].str.extract('(d+)([MH])', expand=True)
            a1 = a[0].astype(int)
            b = df['Time2'].str.extract('(d+)([MH])', expand=True)
            b1 = b[0].astype(int)

            df['Time'] = np.where(a[1] == 'H', a1 * 60, a1) + np.where(b[1] == 'H', b1 * 60, b1)




            print (df)
            Time1 Time2 Time
            0 XY40M XY35M 75
            1 XY5H XY45M 345
            2 XY30M XY20M 50
            3 XY1H XY2H 180


            EDIT:



            a = df['Time1'].str.extract('(d+)([MH])(d*)([M]*)', expand=True)
            a1 = a[[0,2]].replace('', 0).astype(int)
            b = df['Time2'].str.extract('(d+)([MH])(d*)([M]*)', expand=True)
            b1 = b[[0,2]].replace('', 0).astype(int)

            df['Time'] = np.where(a[1] == 'H', a1[0] * 60, a1[0]) + a1[2] +
            np.where(b[1] == 'H', b1[0] * 60, b1[0]) + b1[2]

            print (df)
            Time1 Time2 Time
            0 XY40M XY35M 75
            1 XY5H XY45M 345
            2 XY30M XY20M 50
            3 XY1H XY2H 180
            4 XY1H30M XY2H 210





            share|improve this answer






























              2














              Use str.extract with numpy.where:



              a = df['Time1'].str.extract('(d+[MH])', expand=False)
              a1 = a.str[:-1].astype(int)
              b = df['Time2'].str.extract('(d+[MH])', expand=False)
              b1 = b.str[:-1].astype(int)

              df['Time'] = np.where(a.str[-1] == 'H', a1 * 60, a1) + np.where(b.str[-1] == 'H', b1 * 60, b1)


              Another solution:



              a = df['Time1'].str.extract('(d+)([MH])', expand=True)
              a1 = a[0].astype(int)
              b = df['Time2'].str.extract('(d+)([MH])', expand=True)
              b1 = b[0].astype(int)

              df['Time'] = np.where(a[1] == 'H', a1 * 60, a1) + np.where(b[1] == 'H', b1 * 60, b1)




              print (df)
              Time1 Time2 Time
              0 XY40M XY35M 75
              1 XY5H XY45M 345
              2 XY30M XY20M 50
              3 XY1H XY2H 180


              EDIT:



              a = df['Time1'].str.extract('(d+)([MH])(d*)([M]*)', expand=True)
              a1 = a[[0,2]].replace('', 0).astype(int)
              b = df['Time2'].str.extract('(d+)([MH])(d*)([M]*)', expand=True)
              b1 = b[[0,2]].replace('', 0).astype(int)

              df['Time'] = np.where(a[1] == 'H', a1[0] * 60, a1[0]) + a1[2] +
              np.where(b[1] == 'H', b1[0] * 60, b1[0]) + b1[2]

              print (df)
              Time1 Time2 Time
              0 XY40M XY35M 75
              1 XY5H XY45M 345
              2 XY30M XY20M 50
              3 XY1H XY2H 180
              4 XY1H30M XY2H 210





              share|improve this answer




























                2












                2








                2







                Use str.extract with numpy.where:



                a = df['Time1'].str.extract('(d+[MH])', expand=False)
                a1 = a.str[:-1].astype(int)
                b = df['Time2'].str.extract('(d+[MH])', expand=False)
                b1 = b.str[:-1].astype(int)

                df['Time'] = np.where(a.str[-1] == 'H', a1 * 60, a1) + np.where(b.str[-1] == 'H', b1 * 60, b1)


                Another solution:



                a = df['Time1'].str.extract('(d+)([MH])', expand=True)
                a1 = a[0].astype(int)
                b = df['Time2'].str.extract('(d+)([MH])', expand=True)
                b1 = b[0].astype(int)

                df['Time'] = np.where(a[1] == 'H', a1 * 60, a1) + np.where(b[1] == 'H', b1 * 60, b1)




                print (df)
                Time1 Time2 Time
                0 XY40M XY35M 75
                1 XY5H XY45M 345
                2 XY30M XY20M 50
                3 XY1H XY2H 180


                EDIT:



                a = df['Time1'].str.extract('(d+)([MH])(d*)([M]*)', expand=True)
                a1 = a[[0,2]].replace('', 0).astype(int)
                b = df['Time2'].str.extract('(d+)([MH])(d*)([M]*)', expand=True)
                b1 = b[[0,2]].replace('', 0).astype(int)

                df['Time'] = np.where(a[1] == 'H', a1[0] * 60, a1[0]) + a1[2] +
                np.where(b[1] == 'H', b1[0] * 60, b1[0]) + b1[2]

                print (df)
                Time1 Time2 Time
                0 XY40M XY35M 75
                1 XY5H XY45M 345
                2 XY30M XY20M 50
                3 XY1H XY2H 180
                4 XY1H30M XY2H 210





                share|improve this answer















                Use str.extract with numpy.where:



                a = df['Time1'].str.extract('(d+[MH])', expand=False)
                a1 = a.str[:-1].astype(int)
                b = df['Time2'].str.extract('(d+[MH])', expand=False)
                b1 = b.str[:-1].astype(int)

                df['Time'] = np.where(a.str[-1] == 'H', a1 * 60, a1) + np.where(b.str[-1] == 'H', b1 * 60, b1)


                Another solution:



                a = df['Time1'].str.extract('(d+)([MH])', expand=True)
                a1 = a[0].astype(int)
                b = df['Time2'].str.extract('(d+)([MH])', expand=True)
                b1 = b[0].astype(int)

                df['Time'] = np.where(a[1] == 'H', a1 * 60, a1) + np.where(b[1] == 'H', b1 * 60, b1)




                print (df)
                Time1 Time2 Time
                0 XY40M XY35M 75
                1 XY5H XY45M 345
                2 XY30M XY20M 50
                3 XY1H XY2H 180


                EDIT:



                a = df['Time1'].str.extract('(d+)([MH])(d*)([M]*)', expand=True)
                a1 = a[[0,2]].replace('', 0).astype(int)
                b = df['Time2'].str.extract('(d+)([MH])(d*)([M]*)', expand=True)
                b1 = b[[0,2]].replace('', 0).astype(int)

                df['Time'] = np.where(a[1] == 'H', a1[0] * 60, a1[0]) + a1[2] +
                np.where(b[1] == 'H', b1[0] * 60, b1[0]) + b1[2]

                print (df)
                Time1 Time2 Time
                0 XY40M XY35M 75
                1 XY5H XY45M 345
                2 XY30M XY20M 50
                3 XY1H XY2H 180
                4 XY1H30M XY2H 210






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 11 '18 at 6:37

























                answered Mar 11 '18 at 5:54









                jezraeljezrael

                337k25281357




                337k25281357
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49217070%2fcalculate-time-in-pandas-by-extracting%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'