Pandas dataframe drop columns with no header

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












What's the most pythonic place to drop the columns in a dataframe where the header row is NaN? Preferably inplace.



There may or may not be data in the column.



df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6], np.NaN: [7,np.NaN,9])
df.dropna(axis='columns', inplace=True)


Doesn't do it as it looks at the data in the column.



Wanted output



df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6])


Thanks in advance for the replies.










share|improve this question



























    up vote
    6
    down vote

    favorite












    What's the most pythonic place to drop the columns in a dataframe where the header row is NaN? Preferably inplace.



    There may or may not be data in the column.



    df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6], np.NaN: [7,np.NaN,9])
    df.dropna(axis='columns', inplace=True)


    Doesn't do it as it looks at the data in the column.



    Wanted output



    df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6])


    Thanks in advance for the replies.










    share|improve this question

























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      What's the most pythonic place to drop the columns in a dataframe where the header row is NaN? Preferably inplace.



      There may or may not be data in the column.



      df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6], np.NaN: [7,np.NaN,9])
      df.dropna(axis='columns', inplace=True)


      Doesn't do it as it looks at the data in the column.



      Wanted output



      df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6])


      Thanks in advance for the replies.










      share|improve this question















      What's the most pythonic place to drop the columns in a dataframe where the header row is NaN? Preferably inplace.



      There may or may not be data in the column.



      df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6], np.NaN: [7,np.NaN,9])
      df.dropna(axis='columns', inplace=True)


      Doesn't do it as it looks at the data in the column.



      Wanted output



      df = pd.DataFrame('col1': [1,2,np.NaN], 'col2': [4,5,6])


      Thanks in advance for the replies.







      python pandas dataframe indexing nan






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      jpp

      67.2k173984




      67.2k173984










      asked 1 hour ago









      Joylove

      13210




      13210






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote













          Simply try this



          df.drop(np.nan, axis=1, inplace=True)





          share|improve this answer






















          • why passing as a list?
            – pyd
            1 hour ago






          • 1




            habit :D. doesn't have to. thanks for making perfect!
            – bakka
            42 mins ago

















          up vote
          1
          down vote













          You can use pd.Index.dropna:



          df = df[df.columns.dropna()]

          print(df)

          col1 col2
          0 1.0 4
          1 2.0 5
          2 NaN 6





          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: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            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%2f52568437%2fpandas-dataframe-drop-columns-with-no-header%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote













            Simply try this



            df.drop(np.nan, axis=1, inplace=True)





            share|improve this answer






















            • why passing as a list?
              – pyd
              1 hour ago






            • 1




              habit :D. doesn't have to. thanks for making perfect!
              – bakka
              42 mins ago














            up vote
            4
            down vote













            Simply try this



            df.drop(np.nan, axis=1, inplace=True)





            share|improve this answer






















            • why passing as a list?
              – pyd
              1 hour ago






            • 1




              habit :D. doesn't have to. thanks for making perfect!
              – bakka
              42 mins ago












            up vote
            4
            down vote










            up vote
            4
            down vote









            Simply try this



            df.drop(np.nan, axis=1, inplace=True)





            share|improve this answer














            Simply try this



            df.drop(np.nan, axis=1, inplace=True)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 23 mins ago

























            answered 1 hour ago









            bakka

            3688




            3688











            • why passing as a list?
              – pyd
              1 hour ago






            • 1




              habit :D. doesn't have to. thanks for making perfect!
              – bakka
              42 mins ago
















            • why passing as a list?
              – pyd
              1 hour ago






            • 1




              habit :D. doesn't have to. thanks for making perfect!
              – bakka
              42 mins ago















            why passing as a list?
            – pyd
            1 hour ago




            why passing as a list?
            – pyd
            1 hour ago




            1




            1




            habit :D. doesn't have to. thanks for making perfect!
            – bakka
            42 mins ago




            habit :D. doesn't have to. thanks for making perfect!
            – bakka
            42 mins ago












            up vote
            1
            down vote













            You can use pd.Index.dropna:



            df = df[df.columns.dropna()]

            print(df)

            col1 col2
            0 1.0 4
            1 2.0 5
            2 NaN 6





            share|improve this answer
























              up vote
              1
              down vote













              You can use pd.Index.dropna:



              df = df[df.columns.dropna()]

              print(df)

              col1 col2
              0 1.0 4
              1 2.0 5
              2 NaN 6





              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                You can use pd.Index.dropna:



                df = df[df.columns.dropna()]

                print(df)

                col1 col2
                0 1.0 4
                1 2.0 5
                2 NaN 6





                share|improve this answer












                You can use pd.Index.dropna:



                df = df[df.columns.dropna()]

                print(df)

                col1 col2
                0 1.0 4
                1 2.0 5
                2 NaN 6






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                jpp

                67.2k173984




                67.2k173984



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52568437%2fpandas-dataframe-drop-columns-with-no-header%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery