Pandas Dataframe - select columns with a specific value in a specific row











up vote
1
down vote

favorite












I want to select columns with a specific value (say 1) in a specific row (say first row) for Pandas Dataframe










share|improve this question


























    up vote
    1
    down vote

    favorite












    I want to select columns with a specific value (say 1) in a specific row (say first row) for Pandas Dataframe










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I want to select columns with a specific value (say 1) in a specific row (say first row) for Pandas Dataframe










      share|improve this question













      I want to select columns with a specific value (say 1) in a specific row (say first row) for Pandas Dataframe







      python pandas dataframe






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 11:09









      ghos-h

      718717




      718717
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Use iloc with boolean indexing, for performance is better filtering index not DataFrame and then select index (see performance):



          df = pd.DataFrame({
          'A':list('abcdef'),
          'B':[4,5,4,5,5,4],
          'C':[7,8,9,4,2,3],
          'D':[1,3,5,7,1,0],
          'E':[5,3,6,9,2,4],
          'F':list('aaabbb')
          })

          print (df)
          A B C D E F
          0 a 4 7 1 5 a
          1 b 5 8 3 3 a
          2 c 4 9 5 6 a
          3 d 5 4 7 9 b
          4 e 5 2 1 2 b
          5 f 4 3 0 4 b

          s = df.iloc[0]
          a = s.index[s == 1]
          print (a)
          Index(['D'], dtype='object')

          a = s.index.values[(s == 1)]
          print (a)
          ['D']





          share|improve this answer






























            up vote
            0
            down vote













            You can use iloc to extract a row as a series, then apply your condition:



            row = df.iloc[0]           # extract first row as series
            res = row[res == 1].index # filter for values equal to 1 and get columns via index





            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',
              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%2f53206536%2fpandas-dataframe-select-columns-with-a-specific-value-in-a-specific-row%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








              up vote
              0
              down vote



              accepted










              Use iloc with boolean indexing, for performance is better filtering index not DataFrame and then select index (see performance):



              df = pd.DataFrame({
              'A':list('abcdef'),
              'B':[4,5,4,5,5,4],
              'C':[7,8,9,4,2,3],
              'D':[1,3,5,7,1,0],
              'E':[5,3,6,9,2,4],
              'F':list('aaabbb')
              })

              print (df)
              A B C D E F
              0 a 4 7 1 5 a
              1 b 5 8 3 3 a
              2 c 4 9 5 6 a
              3 d 5 4 7 9 b
              4 e 5 2 1 2 b
              5 f 4 3 0 4 b

              s = df.iloc[0]
              a = s.index[s == 1]
              print (a)
              Index(['D'], dtype='object')

              a = s.index.values[(s == 1)]
              print (a)
              ['D']





              share|improve this answer



























                up vote
                0
                down vote



                accepted










                Use iloc with boolean indexing, for performance is better filtering index not DataFrame and then select index (see performance):



                df = pd.DataFrame({
                'A':list('abcdef'),
                'B':[4,5,4,5,5,4],
                'C':[7,8,9,4,2,3],
                'D':[1,3,5,7,1,0],
                'E':[5,3,6,9,2,4],
                'F':list('aaabbb')
                })

                print (df)
                A B C D E F
                0 a 4 7 1 5 a
                1 b 5 8 3 3 a
                2 c 4 9 5 6 a
                3 d 5 4 7 9 b
                4 e 5 2 1 2 b
                5 f 4 3 0 4 b

                s = df.iloc[0]
                a = s.index[s == 1]
                print (a)
                Index(['D'], dtype='object')

                a = s.index.values[(s == 1)]
                print (a)
                ['D']





                share|improve this answer

























                  up vote
                  0
                  down vote



                  accepted







                  up vote
                  0
                  down vote



                  accepted






                  Use iloc with boolean indexing, for performance is better filtering index not DataFrame and then select index (see performance):



                  df = pd.DataFrame({
                  'A':list('abcdef'),
                  'B':[4,5,4,5,5,4],
                  'C':[7,8,9,4,2,3],
                  'D':[1,3,5,7,1,0],
                  'E':[5,3,6,9,2,4],
                  'F':list('aaabbb')
                  })

                  print (df)
                  A B C D E F
                  0 a 4 7 1 5 a
                  1 b 5 8 3 3 a
                  2 c 4 9 5 6 a
                  3 d 5 4 7 9 b
                  4 e 5 2 1 2 b
                  5 f 4 3 0 4 b

                  s = df.iloc[0]
                  a = s.index[s == 1]
                  print (a)
                  Index(['D'], dtype='object')

                  a = s.index.values[(s == 1)]
                  print (a)
                  ['D']





                  share|improve this answer














                  Use iloc with boolean indexing, for performance is better filtering index not DataFrame and then select index (see performance):



                  df = pd.DataFrame({
                  'A':list('abcdef'),
                  'B':[4,5,4,5,5,4],
                  'C':[7,8,9,4,2,3],
                  'D':[1,3,5,7,1,0],
                  'E':[5,3,6,9,2,4],
                  'F':list('aaabbb')
                  })

                  print (df)
                  A B C D E F
                  0 a 4 7 1 5 a
                  1 b 5 8 3 3 a
                  2 c 4 9 5 6 a
                  3 d 5 4 7 9 b
                  4 e 5 2 1 2 b
                  5 f 4 3 0 4 b

                  s = df.iloc[0]
                  a = s.index[s == 1]
                  print (a)
                  Index(['D'], dtype='object')

                  a = s.index.values[(s == 1)]
                  print (a)
                  ['D']






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 8 at 11:27

























                  answered Nov 8 at 11:16









                  jezrael

                  306k20240316




                  306k20240316
























                      up vote
                      0
                      down vote













                      You can use iloc to extract a row as a series, then apply your condition:



                      row = df.iloc[0]           # extract first row as series
                      res = row[res == 1].index # filter for values equal to 1 and get columns via index





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        You can use iloc to extract a row as a series, then apply your condition:



                        row = df.iloc[0]           # extract first row as series
                        res = row[res == 1].index # filter for values equal to 1 and get columns via index





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You can use iloc to extract a row as a series, then apply your condition:



                          row = df.iloc[0]           # extract first row as series
                          res = row[res == 1].index # filter for values equal to 1 and get columns via index





                          share|improve this answer












                          You can use iloc to extract a row as a series, then apply your condition:



                          row = df.iloc[0]           # extract first row as series
                          res = row[res == 1].index # filter for values equal to 1 and get columns via index






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 8 at 11:14









                          jpp

                          81.7k194795




                          81.7k194795






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206536%2fpandas-dataframe-select-columns-with-a-specific-value-in-a-specific-row%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

                              Schultheiß

                              Verwaltungsgliederung Dänemarks

                              Liste der Kulturdenkmale in Wilsdruff