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
    Feb 5 at 20:26






  • 2





    Can't you transpose and do the filter?

    – Andrew Naguib
    Feb 5 at 20:27











  • Your DataFrame is just a single row?

    – ALollz
    Feb 5 at 20:30











  • not a single row

    – J Oderberg
    Feb 5 at 20:32











  • Please, add a reproducible dataframe :)

    – Andrew Naguib
    Feb 5 at 20:39
















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
    Feb 5 at 20:26






  • 2





    Can't you transpose and do the filter?

    – Andrew Naguib
    Feb 5 at 20:27











  • Your DataFrame is just a single row?

    – ALollz
    Feb 5 at 20:30











  • not a single row

    – J Oderberg
    Feb 5 at 20:32











  • Please, add a reproducible dataframe :)

    – Andrew Naguib
    Feb 5 at 20:39














7












7








7


1






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 Feb 5 at 20:50









Scott Boston

56.1k73157




56.1k73157










asked Feb 5 at 20:24









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
    Feb 5 at 20:26






  • 2





    Can't you transpose and do the filter?

    – Andrew Naguib
    Feb 5 at 20:27











  • Your DataFrame is just a single row?

    – ALollz
    Feb 5 at 20:30











  • not a single row

    – J Oderberg
    Feb 5 at 20:32











  • Please, add a reproducible dataframe :)

    – Andrew Naguib
    Feb 5 at 20:39














  • 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
    Feb 5 at 20:26






  • 2





    Can't you transpose and do the filter?

    – Andrew Naguib
    Feb 5 at 20:27











  • Your DataFrame is just a single row?

    – ALollz
    Feb 5 at 20:30











  • not a single row

    – J Oderberg
    Feb 5 at 20:32











  • Please, add a reproducible dataframe :)

    – Andrew Naguib
    Feb 5 at 20:39








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
Feb 5 at 20:26





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
Feb 5 at 20:26




2




2





Can't you transpose and do the filter?

– Andrew Naguib
Feb 5 at 20:27





Can't you transpose and do the filter?

– Andrew Naguib
Feb 5 at 20:27













Your DataFrame is just a single row?

– ALollz
Feb 5 at 20:30





Your DataFrame is just a single row?

– ALollz
Feb 5 at 20:30













not a single row

– J Oderberg
Feb 5 at 20:32





not a single row

– J Oderberg
Feb 5 at 20:32













Please, add a reproducible dataframe :)

– Andrew Naguib
Feb 5 at 20:39





Please, add a reproducible dataframe :)

– Andrew Naguib
Feb 5 at 20:39












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

























        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 Feb 5 at 20:48









            Scott BostonScott Boston

            56.1k73157




            56.1k73157

























                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 Feb 5 at 20:27









                    Andy HaydenAndy Hayden

                    186k52434422




                    186k52434422























                        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






























                          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




























                            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 Feb 5 at 20:32

























                            answered Feb 5 at 20:27









                            yatuyatu

                            12k31339




                            12k31339






























                                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

                                Probability when a professor distributes a quiz and homework assignment to a class of n students.

                                Aardman Animations

                                Are they similar matrix