How to get specific rows on conditions?










1














For the data as like follows:



Name Stage Start End

Hulk 1 21/10/2018 06:34:15 21/10/2018 07:34:15
Hulk 2 21/10/2018 07:34:15 21/10/2018 07:54:15
Hulk 3 21/10/2018 07:58:15 21/10/2018 08:14:15
Hulk 4 21/10/2018 08:14:15 21/10/2018 08:34:15
Sam A1 21/10/2018 09:34:15 21/10/2018 10:34:15
Sam A2 21/10/2018 10:34:15 21/10/2018 10:45:15
Sam A3 21/10/2018 10:45:15 21/10/2018 11:00:15
Sam A4 21/10/2018 11:00:15 21/10/2018 11:34:15
Bruce 1.1 21/10/2018 11:34:15 21/10/2018 11:45:15
Bruce 1.2 21/10/2018 11:45:15 21/10/2018 12:00:15
Bruce 1.3 21/10/2018 12:00:15 21/10/2018 12:25:15
Bruce 1.4 21/10/2018 12:25:15 21/10/2018 12:45:15
Peter 1 21/10/2018 12:45:15 21/10/2018 01:05:15
Peter 1 21/10/2018 01:05:15 21/10/2018 01:15:15


How can I have first and last instance of Stage for each Name like which are starting with 1 in it and lasting with 4 ?



The dataframe should be in following way :



Name Stage Start End

Hulk 1 21/10/2018 06:34:15 21/10/2018 07:34:15
Hulk 4 21/10/2018 08:14:15 21/10/2018 08:34:15
Sam A1 21/10/2018 09:34:15 21/10/2018 10:34:15
Sam A4 21/10/2018 11:00:15 21/10/2018 11:34:15
Bruce 1.1 21/10/2018 11:34:15 21/10/2018 11:45:15
Bruce 1.4 21/10/2018 12:25:15 21/10/2018 12:45:15


I tried groupby with ([Name,Stage]) but didn't get desired dataframe as above.










share|improve this question


























    1














    For the data as like follows:



    Name Stage Start End

    Hulk 1 21/10/2018 06:34:15 21/10/2018 07:34:15
    Hulk 2 21/10/2018 07:34:15 21/10/2018 07:54:15
    Hulk 3 21/10/2018 07:58:15 21/10/2018 08:14:15
    Hulk 4 21/10/2018 08:14:15 21/10/2018 08:34:15
    Sam A1 21/10/2018 09:34:15 21/10/2018 10:34:15
    Sam A2 21/10/2018 10:34:15 21/10/2018 10:45:15
    Sam A3 21/10/2018 10:45:15 21/10/2018 11:00:15
    Sam A4 21/10/2018 11:00:15 21/10/2018 11:34:15
    Bruce 1.1 21/10/2018 11:34:15 21/10/2018 11:45:15
    Bruce 1.2 21/10/2018 11:45:15 21/10/2018 12:00:15
    Bruce 1.3 21/10/2018 12:00:15 21/10/2018 12:25:15
    Bruce 1.4 21/10/2018 12:25:15 21/10/2018 12:45:15
    Peter 1 21/10/2018 12:45:15 21/10/2018 01:05:15
    Peter 1 21/10/2018 01:05:15 21/10/2018 01:15:15


    How can I have first and last instance of Stage for each Name like which are starting with 1 in it and lasting with 4 ?



    The dataframe should be in following way :



    Name Stage Start End

    Hulk 1 21/10/2018 06:34:15 21/10/2018 07:34:15
    Hulk 4 21/10/2018 08:14:15 21/10/2018 08:34:15
    Sam A1 21/10/2018 09:34:15 21/10/2018 10:34:15
    Sam A4 21/10/2018 11:00:15 21/10/2018 11:34:15
    Bruce 1.1 21/10/2018 11:34:15 21/10/2018 11:45:15
    Bruce 1.4 21/10/2018 12:25:15 21/10/2018 12:45:15


    I tried groupby with ([Name,Stage]) but didn't get desired dataframe as above.










    share|improve this question
























      1












      1








      1


      1





      For the data as like follows:



      Name Stage Start End

      Hulk 1 21/10/2018 06:34:15 21/10/2018 07:34:15
      Hulk 2 21/10/2018 07:34:15 21/10/2018 07:54:15
      Hulk 3 21/10/2018 07:58:15 21/10/2018 08:14:15
      Hulk 4 21/10/2018 08:14:15 21/10/2018 08:34:15
      Sam A1 21/10/2018 09:34:15 21/10/2018 10:34:15
      Sam A2 21/10/2018 10:34:15 21/10/2018 10:45:15
      Sam A3 21/10/2018 10:45:15 21/10/2018 11:00:15
      Sam A4 21/10/2018 11:00:15 21/10/2018 11:34:15
      Bruce 1.1 21/10/2018 11:34:15 21/10/2018 11:45:15
      Bruce 1.2 21/10/2018 11:45:15 21/10/2018 12:00:15
      Bruce 1.3 21/10/2018 12:00:15 21/10/2018 12:25:15
      Bruce 1.4 21/10/2018 12:25:15 21/10/2018 12:45:15
      Peter 1 21/10/2018 12:45:15 21/10/2018 01:05:15
      Peter 1 21/10/2018 01:05:15 21/10/2018 01:15:15


      How can I have first and last instance of Stage for each Name like which are starting with 1 in it and lasting with 4 ?



      The dataframe should be in following way :



      Name Stage Start End

      Hulk 1 21/10/2018 06:34:15 21/10/2018 07:34:15
      Hulk 4 21/10/2018 08:14:15 21/10/2018 08:34:15
      Sam A1 21/10/2018 09:34:15 21/10/2018 10:34:15
      Sam A4 21/10/2018 11:00:15 21/10/2018 11:34:15
      Bruce 1.1 21/10/2018 11:34:15 21/10/2018 11:45:15
      Bruce 1.4 21/10/2018 12:25:15 21/10/2018 12:45:15


      I tried groupby with ([Name,Stage]) but didn't get desired dataframe as above.










      share|improve this question













      For the data as like follows:



      Name Stage Start End

      Hulk 1 21/10/2018 06:34:15 21/10/2018 07:34:15
      Hulk 2 21/10/2018 07:34:15 21/10/2018 07:54:15
      Hulk 3 21/10/2018 07:58:15 21/10/2018 08:14:15
      Hulk 4 21/10/2018 08:14:15 21/10/2018 08:34:15
      Sam A1 21/10/2018 09:34:15 21/10/2018 10:34:15
      Sam A2 21/10/2018 10:34:15 21/10/2018 10:45:15
      Sam A3 21/10/2018 10:45:15 21/10/2018 11:00:15
      Sam A4 21/10/2018 11:00:15 21/10/2018 11:34:15
      Bruce 1.1 21/10/2018 11:34:15 21/10/2018 11:45:15
      Bruce 1.2 21/10/2018 11:45:15 21/10/2018 12:00:15
      Bruce 1.3 21/10/2018 12:00:15 21/10/2018 12:25:15
      Bruce 1.4 21/10/2018 12:25:15 21/10/2018 12:45:15
      Peter 1 21/10/2018 12:45:15 21/10/2018 01:05:15
      Peter 1 21/10/2018 01:05:15 21/10/2018 01:15:15


      How can I have first and last instance of Stage for each Name like which are starting with 1 in it and lasting with 4 ?



      The dataframe should be in following way :



      Name Stage Start End

      Hulk 1 21/10/2018 06:34:15 21/10/2018 07:34:15
      Hulk 4 21/10/2018 08:14:15 21/10/2018 08:34:15
      Sam A1 21/10/2018 09:34:15 21/10/2018 10:34:15
      Sam A4 21/10/2018 11:00:15 21/10/2018 11:34:15
      Bruce 1.1 21/10/2018 11:34:15 21/10/2018 11:45:15
      Bruce 1.4 21/10/2018 12:25:15 21/10/2018 12:45:15


      I tried groupby with ([Name,Stage]) but didn't get desired dataframe as above.







      python-2.7 pandas dataframe row slice






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 6:58









      Ranjan raghav

      153




      153






















          1 Answer
          1






          active

          oldest

          votes


















          3














          Use duplicated with str.contains with boolean indexing for return necessary rows first and then value_counts with map for filter only 2 row groups:



          m1 = ~df['Name'].duplicated()
          m2 = df['Stage'].str.contains('1')

          m3 = ~df['Name'].duplicated(keep='last')
          m4 = df['Stage'].str.contains('4')

          df1 = df[(m1 & m2) | (m3 & m4)].copy()

          df1 = df1[df1['Name'].map(df1['Name'].value_counts()) == 2]
          print (df1)
          Name Stage Start End
          0 Hulk 1 21/10/2018 06:34:15 21/10/2018 07:34:15
          3 Hulk 4 21/10/2018 08:14:15 21/10/2018 08:34:15
          4 Sam A1 21/10/2018 09:34:15 21/10/2018 10:34:15
          7 Sam A4 21/10/2018 11:00:15 21/10/2018 11:34:15
          8 Bruce 1.1 21/10/2018 11:34:15 21/10/2018 11:45:15
          11 Bruce 1.4 21/10/2018 12:25:15 21/10/2018 12:45:15





          share|improve this answer






















          • ++ve for nice solution sir, could you please do add how are you creating df too in your code as a newbie I could get a complete picture of this solution too then, will be grateful to you, thanks again.
            – RavinderSingh13
            Nov 12 at 7:42






          • 1




            @RavinderSingh13 - Use df = pd.read_clipboard(sep='s2,') - separator is regex 2 or more whitespaces
            – jezrael
            Nov 12 at 7:43






          • 1




            Thanks a TON sir, it helped me. You are AWESOME.
            – RavinderSingh13
            Nov 12 at 7:53










          • @jezrael Thanks jezrael for the lovely solution, really admire your conceptual strength : )
            – Ranjan raghav
            Nov 12 at 8:10






          • 1




            @Ranjanraghav - You are welcome!
            – jezrael
            Nov 12 at 8:11










          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%2f53257220%2fhow-to-get-specific-rows-on-conditions%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









          3














          Use duplicated with str.contains with boolean indexing for return necessary rows first and then value_counts with map for filter only 2 row groups:



          m1 = ~df['Name'].duplicated()
          m2 = df['Stage'].str.contains('1')

          m3 = ~df['Name'].duplicated(keep='last')
          m4 = df['Stage'].str.contains('4')

          df1 = df[(m1 & m2) | (m3 & m4)].copy()

          df1 = df1[df1['Name'].map(df1['Name'].value_counts()) == 2]
          print (df1)
          Name Stage Start End
          0 Hulk 1 21/10/2018 06:34:15 21/10/2018 07:34:15
          3 Hulk 4 21/10/2018 08:14:15 21/10/2018 08:34:15
          4 Sam A1 21/10/2018 09:34:15 21/10/2018 10:34:15
          7 Sam A4 21/10/2018 11:00:15 21/10/2018 11:34:15
          8 Bruce 1.1 21/10/2018 11:34:15 21/10/2018 11:45:15
          11 Bruce 1.4 21/10/2018 12:25:15 21/10/2018 12:45:15





          share|improve this answer






















          • ++ve for nice solution sir, could you please do add how are you creating df too in your code as a newbie I could get a complete picture of this solution too then, will be grateful to you, thanks again.
            – RavinderSingh13
            Nov 12 at 7:42






          • 1




            @RavinderSingh13 - Use df = pd.read_clipboard(sep='s2,') - separator is regex 2 or more whitespaces
            – jezrael
            Nov 12 at 7:43






          • 1




            Thanks a TON sir, it helped me. You are AWESOME.
            – RavinderSingh13
            Nov 12 at 7:53










          • @jezrael Thanks jezrael for the lovely solution, really admire your conceptual strength : )
            – Ranjan raghav
            Nov 12 at 8:10






          • 1




            @Ranjanraghav - You are welcome!
            – jezrael
            Nov 12 at 8:11















          3














          Use duplicated with str.contains with boolean indexing for return necessary rows first and then value_counts with map for filter only 2 row groups:



          m1 = ~df['Name'].duplicated()
          m2 = df['Stage'].str.contains('1')

          m3 = ~df['Name'].duplicated(keep='last')
          m4 = df['Stage'].str.contains('4')

          df1 = df[(m1 & m2) | (m3 & m4)].copy()

          df1 = df1[df1['Name'].map(df1['Name'].value_counts()) == 2]
          print (df1)
          Name Stage Start End
          0 Hulk 1 21/10/2018 06:34:15 21/10/2018 07:34:15
          3 Hulk 4 21/10/2018 08:14:15 21/10/2018 08:34:15
          4 Sam A1 21/10/2018 09:34:15 21/10/2018 10:34:15
          7 Sam A4 21/10/2018 11:00:15 21/10/2018 11:34:15
          8 Bruce 1.1 21/10/2018 11:34:15 21/10/2018 11:45:15
          11 Bruce 1.4 21/10/2018 12:25:15 21/10/2018 12:45:15





          share|improve this answer






















          • ++ve for nice solution sir, could you please do add how are you creating df too in your code as a newbie I could get a complete picture of this solution too then, will be grateful to you, thanks again.
            – RavinderSingh13
            Nov 12 at 7:42






          • 1




            @RavinderSingh13 - Use df = pd.read_clipboard(sep='s2,') - separator is regex 2 or more whitespaces
            – jezrael
            Nov 12 at 7:43






          • 1




            Thanks a TON sir, it helped me. You are AWESOME.
            – RavinderSingh13
            Nov 12 at 7:53










          • @jezrael Thanks jezrael for the lovely solution, really admire your conceptual strength : )
            – Ranjan raghav
            Nov 12 at 8:10






          • 1




            @Ranjanraghav - You are welcome!
            – jezrael
            Nov 12 at 8:11













          3












          3








          3






          Use duplicated with str.contains with boolean indexing for return necessary rows first and then value_counts with map for filter only 2 row groups:



          m1 = ~df['Name'].duplicated()
          m2 = df['Stage'].str.contains('1')

          m3 = ~df['Name'].duplicated(keep='last')
          m4 = df['Stage'].str.contains('4')

          df1 = df[(m1 & m2) | (m3 & m4)].copy()

          df1 = df1[df1['Name'].map(df1['Name'].value_counts()) == 2]
          print (df1)
          Name Stage Start End
          0 Hulk 1 21/10/2018 06:34:15 21/10/2018 07:34:15
          3 Hulk 4 21/10/2018 08:14:15 21/10/2018 08:34:15
          4 Sam A1 21/10/2018 09:34:15 21/10/2018 10:34:15
          7 Sam A4 21/10/2018 11:00:15 21/10/2018 11:34:15
          8 Bruce 1.1 21/10/2018 11:34:15 21/10/2018 11:45:15
          11 Bruce 1.4 21/10/2018 12:25:15 21/10/2018 12:45:15





          share|improve this answer














          Use duplicated with str.contains with boolean indexing for return necessary rows first and then value_counts with map for filter only 2 row groups:



          m1 = ~df['Name'].duplicated()
          m2 = df['Stage'].str.contains('1')

          m3 = ~df['Name'].duplicated(keep='last')
          m4 = df['Stage'].str.contains('4')

          df1 = df[(m1 & m2) | (m3 & m4)].copy()

          df1 = df1[df1['Name'].map(df1['Name'].value_counts()) == 2]
          print (df1)
          Name Stage Start End
          0 Hulk 1 21/10/2018 06:34:15 21/10/2018 07:34:15
          3 Hulk 4 21/10/2018 08:14:15 21/10/2018 08:34:15
          4 Sam A1 21/10/2018 09:34:15 21/10/2018 10:34:15
          7 Sam A4 21/10/2018 11:00:15 21/10/2018 11:34:15
          8 Bruce 1.1 21/10/2018 11:34:15 21/10/2018 11:45:15
          11 Bruce 1.4 21/10/2018 12:25:15 21/10/2018 12:45:15






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 7:25

























          answered Nov 12 at 7:16









          jezrael

          318k22258336




          318k22258336











          • ++ve for nice solution sir, could you please do add how are you creating df too in your code as a newbie I could get a complete picture of this solution too then, will be grateful to you, thanks again.
            – RavinderSingh13
            Nov 12 at 7:42






          • 1




            @RavinderSingh13 - Use df = pd.read_clipboard(sep='s2,') - separator is regex 2 or more whitespaces
            – jezrael
            Nov 12 at 7:43






          • 1




            Thanks a TON sir, it helped me. You are AWESOME.
            – RavinderSingh13
            Nov 12 at 7:53










          • @jezrael Thanks jezrael for the lovely solution, really admire your conceptual strength : )
            – Ranjan raghav
            Nov 12 at 8:10






          • 1




            @Ranjanraghav - You are welcome!
            – jezrael
            Nov 12 at 8:11
















          • ++ve for nice solution sir, could you please do add how are you creating df too in your code as a newbie I could get a complete picture of this solution too then, will be grateful to you, thanks again.
            – RavinderSingh13
            Nov 12 at 7:42






          • 1




            @RavinderSingh13 - Use df = pd.read_clipboard(sep='s2,') - separator is regex 2 or more whitespaces
            – jezrael
            Nov 12 at 7:43






          • 1




            Thanks a TON sir, it helped me. You are AWESOME.
            – RavinderSingh13
            Nov 12 at 7:53










          • @jezrael Thanks jezrael for the lovely solution, really admire your conceptual strength : )
            – Ranjan raghav
            Nov 12 at 8:10






          • 1




            @Ranjanraghav - You are welcome!
            – jezrael
            Nov 12 at 8:11















          ++ve for nice solution sir, could you please do add how are you creating df too in your code as a newbie I could get a complete picture of this solution too then, will be grateful to you, thanks again.
          – RavinderSingh13
          Nov 12 at 7:42




          ++ve for nice solution sir, could you please do add how are you creating df too in your code as a newbie I could get a complete picture of this solution too then, will be grateful to you, thanks again.
          – RavinderSingh13
          Nov 12 at 7:42




          1




          1




          @RavinderSingh13 - Use df = pd.read_clipboard(sep='s2,') - separator is regex 2 or more whitespaces
          – jezrael
          Nov 12 at 7:43




          @RavinderSingh13 - Use df = pd.read_clipboard(sep='s2,') - separator is regex 2 or more whitespaces
          – jezrael
          Nov 12 at 7:43




          1




          1




          Thanks a TON sir, it helped me. You are AWESOME.
          – RavinderSingh13
          Nov 12 at 7:53




          Thanks a TON sir, it helped me. You are AWESOME.
          – RavinderSingh13
          Nov 12 at 7:53












          @jezrael Thanks jezrael for the lovely solution, really admire your conceptual strength : )
          – Ranjan raghav
          Nov 12 at 8:10




          @jezrael Thanks jezrael for the lovely solution, really admire your conceptual strength : )
          – Ranjan raghav
          Nov 12 at 8:10




          1




          1




          @Ranjanraghav - You are welcome!
          – jezrael
          Nov 12 at 8:11




          @Ranjanraghav - You are welcome!
          – jezrael
          Nov 12 at 8:11

















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53257220%2fhow-to-get-specific-rows-on-conditions%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto