In Pandas how can I reduce the rows so that I only accept the row with the first true in every sub group of...












0















As an example, consider the following:



Input



a b   c  bool
1 243 0 true
1 253 1 false
1 267 0 true
1 245 0 true
1 234 0 false
1 255 0 true
1 275 0 true
1 295 0 true


I want the output to be the following:



a b   c  bool
1 243 0 true
1 253 1 false
1 267 0 true
1 234 0 false
1 255 0 true









share|improve this question



























    0















    As an example, consider the following:



    Input



    a b   c  bool
    1 243 0 true
    1 253 1 false
    1 267 0 true
    1 245 0 true
    1 234 0 false
    1 255 0 true
    1 275 0 true
    1 295 0 true


    I want the output to be the following:



    a b   c  bool
    1 243 0 true
    1 253 1 false
    1 267 0 true
    1 234 0 false
    1 255 0 true









    share|improve this question

























      0












      0








      0








      As an example, consider the following:



      Input



      a b   c  bool
      1 243 0 true
      1 253 1 false
      1 267 0 true
      1 245 0 true
      1 234 0 false
      1 255 0 true
      1 275 0 true
      1 295 0 true


      I want the output to be the following:



      a b   c  bool
      1 243 0 true
      1 253 1 false
      1 267 0 true
      1 234 0 false
      1 255 0 true









      share|improve this question














      As an example, consider the following:



      Input



      a b   c  bool
      1 243 0 true
      1 253 1 false
      1 267 0 true
      1 245 0 true
      1 234 0 false
      1 255 0 true
      1 275 0 true
      1 295 0 true


      I want the output to be the following:



      a b   c  bool
      1 243 0 true
      1 253 1 false
      1 267 0 true
      1 234 0 false
      1 255 0 true






      python pandas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 23:09









      NothingNothing

      5211




      5211
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Use some boolean masking for this and do it in one line:



          df.loc[(df['bool'] != df['bool'].shift(1))]


          out:



          >>> df.loc[(df['bool'] != df['bool'].shift(1))]
          a b c bool
          0 1 243 0 True
          1 1 253 1 False
          2 1 267 0 True
          4 1 234 0 False
          5 1 255 0 True





          share|improve this answer































            1














            IIUC



            df.groupby(df['bool'].ne(True).cumsum()).head(2)
            Out[201]:
            a b c bool
            0 1 243 0 True
            1 1 253 1 False
            2 1 267 0 True
            4 1 234 0 False
            5 1 255 0 True





            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%2f53310109%2fin-pandas-how-can-i-reduce-the-rows-so-that-i-only-accept-the-row-with-the-first%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              Use some boolean masking for this and do it in one line:



              df.loc[(df['bool'] != df['bool'].shift(1))]


              out:



              >>> df.loc[(df['bool'] != df['bool'].shift(1))]
              a b c bool
              0 1 243 0 True
              1 1 253 1 False
              2 1 267 0 True
              4 1 234 0 False
              5 1 255 0 True





              share|improve this answer




























                1














                Use some boolean masking for this and do it in one line:



                df.loc[(df['bool'] != df['bool'].shift(1))]


                out:



                >>> df.loc[(df['bool'] != df['bool'].shift(1))]
                a b c bool
                0 1 243 0 True
                1 1 253 1 False
                2 1 267 0 True
                4 1 234 0 False
                5 1 255 0 True





                share|improve this answer


























                  1












                  1








                  1







                  Use some boolean masking for this and do it in one line:



                  df.loc[(df['bool'] != df['bool'].shift(1))]


                  out:



                  >>> df.loc[(df['bool'] != df['bool'].shift(1))]
                  a b c bool
                  0 1 243 0 True
                  1 1 253 1 False
                  2 1 267 0 True
                  4 1 234 0 False
                  5 1 255 0 True





                  share|improve this answer













                  Use some boolean masking for this and do it in one line:



                  df.loc[(df['bool'] != df['bool'].shift(1))]


                  out:



                  >>> df.loc[(df['bool'] != df['bool'].shift(1))]
                  a b c bool
                  0 1 243 0 True
                  1 1 253 1 False
                  2 1 267 0 True
                  4 1 234 0 False
                  5 1 255 0 True






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 23:18









                  d_kennetzd_kennetz

                  2,2803724




                  2,2803724

























                      1














                      IIUC



                      df.groupby(df['bool'].ne(True).cumsum()).head(2)
                      Out[201]:
                      a b c bool
                      0 1 243 0 True
                      1 1 253 1 False
                      2 1 267 0 True
                      4 1 234 0 False
                      5 1 255 0 True





                      share|improve this answer




























                        1














                        IIUC



                        df.groupby(df['bool'].ne(True).cumsum()).head(2)
                        Out[201]:
                        a b c bool
                        0 1 243 0 True
                        1 1 253 1 False
                        2 1 267 0 True
                        4 1 234 0 False
                        5 1 255 0 True





                        share|improve this answer


























                          1












                          1








                          1







                          IIUC



                          df.groupby(df['bool'].ne(True).cumsum()).head(2)
                          Out[201]:
                          a b c bool
                          0 1 243 0 True
                          1 1 253 1 False
                          2 1 267 0 True
                          4 1 234 0 False
                          5 1 255 0 True





                          share|improve this answer













                          IIUC



                          df.groupby(df['bool'].ne(True).cumsum()).head(2)
                          Out[201]:
                          a b c bool
                          0 1 243 0 True
                          1 1 253 1 False
                          2 1 267 0 True
                          4 1 234 0 False
                          5 1 255 0 True






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 14 '18 at 23:20









                          Wen-BenWen-Ben

                          112k83367




                          112k83367






























                              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%2f53310109%2fin-pandas-how-can-i-reduce-the-rows-so-that-i-only-accept-the-row-with-the-first%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

                              List item for chat from Array inside array React Native

                              Thiostrepton

                              Caerphilly