How to reduce columns in dataframe pandas












7















Here how the datalooks like in df dataframe:



        A   B   C   D
0.js 2 1 1 -1
1.js 3 -5 1 -4
total 5 -4 2 -5


And I would get new dataframe df1:



        A     C
0.js 2 1
1.js 3 1
total 5 2


So basically it should look like this:
df1 = df[df["total"] > 0]
but it should filter on row instead of column and I can't figure it out..










share|improve this question




















  • 2





    Do you want to only keep the columns where the condition is met ALWAYS, even if the condition is not met once in another column.

    – Edeki Okoh
    4 hours ago






  • 2





    Can't you transpose and do the filter?

    – Andrew Naguib
    4 hours ago











  • Your DataFrame is just a single row?

    – ALollz
    4 hours ago






  • 2





    Then please provide a Minimal, Complete, and Verifiable example with multiple lines and your expected output, because I don't think the current answers are going to do what you are looking for.

    – ALollz
    4 hours ago








  • 2





    I've updated the question, thanks for the guidance.

    – J Oderberg
    4 hours ago
















7















Here how the datalooks like in df dataframe:



        A   B   C   D
0.js 2 1 1 -1
1.js 3 -5 1 -4
total 5 -4 2 -5


And I would get new dataframe df1:



        A     C
0.js 2 1
1.js 3 1
total 5 2


So basically it should look like this:
df1 = df[df["total"] > 0]
but it should filter on row instead of column and I can't figure it out..










share|improve this question




















  • 2





    Do you want to only keep the columns where the condition is met ALWAYS, even if the condition is not met once in another column.

    – Edeki Okoh
    4 hours ago






  • 2





    Can't you transpose and do the filter?

    – Andrew Naguib
    4 hours ago











  • Your DataFrame is just a single row?

    – ALollz
    4 hours ago






  • 2





    Then please provide a Minimal, Complete, and Verifiable example with multiple lines and your expected output, because I don't think the current answers are going to do what you are looking for.

    – ALollz
    4 hours ago








  • 2





    I've updated the question, thanks for the guidance.

    – J Oderberg
    4 hours ago














7












7








7








Here how the datalooks like in df dataframe:



        A   B   C   D
0.js 2 1 1 -1
1.js 3 -5 1 -4
total 5 -4 2 -5


And I would get new dataframe df1:



        A     C
0.js 2 1
1.js 3 1
total 5 2


So basically it should look like this:
df1 = df[df["total"] > 0]
but it should filter on row instead of column and I can't figure it out..










share|improve this question
















Here how the datalooks like in df dataframe:



        A   B   C   D
0.js 2 1 1 -1
1.js 3 -5 1 -4
total 5 -4 2 -5


And I would get new dataframe df1:



        A     C
0.js 2 1
1.js 3 1
total 5 2


So basically it should look like this:
df1 = df[df["total"] > 0]
but it should filter on row instead of column and I can't figure it out..







python pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 4 hours ago









Scott Boston

54.1k73056




54.1k73056










asked 4 hours ago









J OderbergJ Oderberg

404




404








  • 2





    Do you want to only keep the columns where the condition is met ALWAYS, even if the condition is not met once in another column.

    – Edeki Okoh
    4 hours ago






  • 2





    Can't you transpose and do the filter?

    – Andrew Naguib
    4 hours ago











  • Your DataFrame is just a single row?

    – ALollz
    4 hours ago






  • 2





    Then please provide a Minimal, Complete, and Verifiable example with multiple lines and your expected output, because I don't think the current answers are going to do what you are looking for.

    – ALollz
    4 hours ago








  • 2





    I've updated the question, thanks for the guidance.

    – J Oderberg
    4 hours ago














  • 2





    Do you want to only keep the columns where the condition is met ALWAYS, even if the condition is not met once in another column.

    – Edeki Okoh
    4 hours ago






  • 2





    Can't you transpose and do the filter?

    – Andrew Naguib
    4 hours ago











  • Your DataFrame is just a single row?

    – ALollz
    4 hours ago






  • 2





    Then please provide a Minimal, Complete, and Verifiable example with multiple lines and your expected output, because I don't think the current answers are going to do what you are looking for.

    – ALollz
    4 hours ago








  • 2





    I've updated the question, thanks for the guidance.

    – J Oderberg
    4 hours ago








2




2





Do you want to only keep the columns where the condition is met ALWAYS, even if the condition is not met once in another column.

– Edeki Okoh
4 hours ago





Do you want to only keep the columns where the condition is met ALWAYS, even if the condition is not met once in another column.

– Edeki Okoh
4 hours ago




2




2





Can't you transpose and do the filter?

– Andrew Naguib
4 hours ago





Can't you transpose and do the filter?

– Andrew Naguib
4 hours ago













Your DataFrame is just a single row?

– ALollz
4 hours ago





Your DataFrame is just a single row?

– ALollz
4 hours ago




2




2





Then please provide a Minimal, Complete, and Verifiable example with multiple lines and your expected output, because I don't think the current answers are going to do what you are looking for.

– ALollz
4 hours ago







Then please provide a Minimal, Complete, and Verifiable example with multiple lines and your expected output, because I don't think the current answers are going to do what you are looking for.

– ALollz
4 hours ago






2




2





I've updated the question, thanks for the guidance.

– J Oderberg
4 hours ago





I've updated the question, thanks for the guidance.

– J Oderberg
4 hours ago












3 Answers
3






active

oldest

votes


















2














You can use, loc with boolean indexing or reindex:



df.loc[:, df.columns[(df.loc['total'] > 0)]]


OR



df.reindex(df.columns[(df.loc['total'] > 0)], axis=1)


Output:



       A  C
0.js 2 1
1.js 3 1
total 5 2





share|improve this answer































    4














    You want to use .loc[:, column_mask] i.e.



    In [11]: df.loc[:, df.sum() > 0]
    Out[11]:
    A C
    total 5 2

    # or

    In [12]: df.loc[:, df.iloc[0] > 0]
    Out[12]:
    A C
    total 5 2





    share|improve this answer































      4














      Use .where to set negative values to NaN and then dropna setting axis = 1:



      df.where(df.gt(0)).dropna(axis=1)

      A C
      total 5 2





      share|improve this answer





















      • 1





        Okay, that was clever +1

        – harvpan
        4 hours ago











      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%2f54542470%2fhow-to-reduce-columns-in-dataframe-pandas%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You can use, loc with boolean indexing or reindex:



      df.loc[:, df.columns[(df.loc['total'] > 0)]]


      OR



      df.reindex(df.columns[(df.loc['total'] > 0)], axis=1)


      Output:



             A  C
      0.js 2 1
      1.js 3 1
      total 5 2





      share|improve this answer




























        2














        You can use, loc with boolean indexing or reindex:



        df.loc[:, df.columns[(df.loc['total'] > 0)]]


        OR



        df.reindex(df.columns[(df.loc['total'] > 0)], axis=1)


        Output:



               A  C
        0.js 2 1
        1.js 3 1
        total 5 2





        share|improve this answer


























          2












          2








          2







          You can use, loc with boolean indexing or reindex:



          df.loc[:, df.columns[(df.loc['total'] > 0)]]


          OR



          df.reindex(df.columns[(df.loc['total'] > 0)], axis=1)


          Output:



                 A  C
          0.js 2 1
          1.js 3 1
          total 5 2





          share|improve this answer













          You can use, loc with boolean indexing or reindex:



          df.loc[:, df.columns[(df.loc['total'] > 0)]]


          OR



          df.reindex(df.columns[(df.loc['total'] > 0)], axis=1)


          Output:



                 A  C
          0.js 2 1
          1.js 3 1
          total 5 2






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 4 hours ago









          Scott BostonScott Boston

          54.1k73056




          54.1k73056

























              4














              You want to use .loc[:, column_mask] i.e.



              In [11]: df.loc[:, df.sum() > 0]
              Out[11]:
              A C
              total 5 2

              # or

              In [12]: df.loc[:, df.iloc[0] > 0]
              Out[12]:
              A C
              total 5 2





              share|improve this answer




























                4














                You want to use .loc[:, column_mask] i.e.



                In [11]: df.loc[:, df.sum() > 0]
                Out[11]:
                A C
                total 5 2

                # or

                In [12]: df.loc[:, df.iloc[0] > 0]
                Out[12]:
                A C
                total 5 2





                share|improve this answer


























                  4












                  4








                  4







                  You want to use .loc[:, column_mask] i.e.



                  In [11]: df.loc[:, df.sum() > 0]
                  Out[11]:
                  A C
                  total 5 2

                  # or

                  In [12]: df.loc[:, df.iloc[0] > 0]
                  Out[12]:
                  A C
                  total 5 2





                  share|improve this answer













                  You want to use .loc[:, column_mask] i.e.



                  In [11]: df.loc[:, df.sum() > 0]
                  Out[11]:
                  A C
                  total 5 2

                  # or

                  In [12]: df.loc[:, df.iloc[0] > 0]
                  Out[12]:
                  A C
                  total 5 2






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 4 hours ago









                  Andy HaydenAndy Hayden

                  182k52429417




                  182k52429417























                      4














                      Use .where to set negative values to NaN and then dropna setting axis = 1:



                      df.where(df.gt(0)).dropna(axis=1)

                      A C
                      total 5 2





                      share|improve this answer





















                      • 1





                        Okay, that was clever +1

                        – harvpan
                        4 hours ago
















                      4














                      Use .where to set negative values to NaN and then dropna setting axis = 1:



                      df.where(df.gt(0)).dropna(axis=1)

                      A C
                      total 5 2





                      share|improve this answer





















                      • 1





                        Okay, that was clever +1

                        – harvpan
                        4 hours ago














                      4












                      4








                      4







                      Use .where to set negative values to NaN and then dropna setting axis = 1:



                      df.where(df.gt(0)).dropna(axis=1)

                      A C
                      total 5 2





                      share|improve this answer















                      Use .where to set negative values to NaN and then dropna setting axis = 1:



                      df.where(df.gt(0)).dropna(axis=1)

                      A C
                      total 5 2






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 4 hours ago

























                      answered 4 hours ago









                      yatuyatu

                      8,2011926




                      8,2011926








                      • 1





                        Okay, that was clever +1

                        – harvpan
                        4 hours ago














                      • 1





                        Okay, that was clever +1

                        – harvpan
                        4 hours ago








                      1




                      1





                      Okay, that was clever +1

                      – harvpan
                      4 hours ago





                      Okay, that was clever +1

                      – harvpan
                      4 hours ago


















                      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%2f54542470%2fhow-to-reduce-columns-in-dataframe-pandas%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

                      Ponta tanko

                      Tantalo (mitologio)

                      Erzsébet Schaár