Pandas read_html how to drop Index?












1















I'm struggling on removing the index column from the dataframe.



Usually when I read a csv file, I can set the index = False or index_col = 0, and that removes the index column. But I can't do that when reading html for some reason. Any ideas? I've also tried reset_index(drop=True). I don't want to set any of the columns to an index.



path = 'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M' 

canada = pd.read_html(path)

cn_table=canada[0]









share|improve this question





























    1















    I'm struggling on removing the index column from the dataframe.



    Usually when I read a csv file, I can set the index = False or index_col = 0, and that removes the index column. But I can't do that when reading html for some reason. Any ideas? I've also tried reset_index(drop=True). I don't want to set any of the columns to an index.



    path = 'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M' 

    canada = pd.read_html(path)

    cn_table=canada[0]









    share|improve this question



























      1












      1








      1








      I'm struggling on removing the index column from the dataframe.



      Usually when I read a csv file, I can set the index = False or index_col = 0, and that removes the index column. But I can't do that when reading html for some reason. Any ideas? I've also tried reset_index(drop=True). I don't want to set any of the columns to an index.



      path = 'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M' 

      canada = pd.read_html(path)

      cn_table=canada[0]









      share|improve this question
















      I'm struggling on removing the index column from the dataframe.



      Usually when I read a csv file, I can set the index = False or index_col = 0, and that removes the index column. But I can't do that when reading html for some reason. Any ideas? I've also tried reset_index(drop=True). I don't want to set any of the columns to an index.



      path = 'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M' 

      canada = pd.read_html(path)

      cn_table=canada[0]






      pandas dataframe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 20 at 4:07









      anky_91

      4,3192319




      4,3192319










      asked Jan 20 at 3:59









      user10939484user10939484

      124




      124
























          1 Answer
          1






          active

          oldest

          votes


















          0














          IIUC , you want the 1st row as headers, Use header=0:



          canada = pd.read_html(r'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M',header =0, flavor = 'bs4')


          Or:



          canada = pd.read_html(r'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M',header =0)

          cn_table=canada[0]
          >>cn_table

          Postcode Borough Neighbourhood
          0 M1A Not assigned Not assigned
          1 M2A Not assigned Not assigned
          2 M3A North York Parkwoods
          3 M4A North York Victoria Village
          4 M5A Downtown Toronto Harbourfront
          5 M5A Downtown Toronto Regent Park

          ... ... ... ...
          288 M9Z Not assigned Not assigned


          To save the dataframe to csv without index use:



          cn_table.to_csv('path+filename.csv',index=False)





          share|improve this answer


























          • The output still contains an index column.

            – user10939484
            Jan 20 at 4:38











          • @user10939484 can you share your expected output plz?

            – anky_91
            Jan 20 at 4:38











          • @user10939484 did you mean print(cn_table.to_string(index=False)) ??

            – anky_91
            Jan 20 at 4:45











          • No, I'd like to permanently modify the dataframe to output only the 3 columns without the index to the left. Same as your output without the index.

            – user10939484
            Jan 20 at 4:56






          • 1





            Thanks, I was not aware of that. Not bothering me. The assignment I'm working on has a finished dataframe image that does not have an index so it's been annoying me, making me think I was just missing something obvious.

            – user10939484
            Jan 20 at 5:15













          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%2f54273465%2fpandas-read-html-how-to-drop-index%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          IIUC , you want the 1st row as headers, Use header=0:



          canada = pd.read_html(r'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M',header =0, flavor = 'bs4')


          Or:



          canada = pd.read_html(r'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M',header =0)

          cn_table=canada[0]
          >>cn_table

          Postcode Borough Neighbourhood
          0 M1A Not assigned Not assigned
          1 M2A Not assigned Not assigned
          2 M3A North York Parkwoods
          3 M4A North York Victoria Village
          4 M5A Downtown Toronto Harbourfront
          5 M5A Downtown Toronto Regent Park

          ... ... ... ...
          288 M9Z Not assigned Not assigned


          To save the dataframe to csv without index use:



          cn_table.to_csv('path+filename.csv',index=False)





          share|improve this answer


























          • The output still contains an index column.

            – user10939484
            Jan 20 at 4:38











          • @user10939484 can you share your expected output plz?

            – anky_91
            Jan 20 at 4:38











          • @user10939484 did you mean print(cn_table.to_string(index=False)) ??

            – anky_91
            Jan 20 at 4:45











          • No, I'd like to permanently modify the dataframe to output only the 3 columns without the index to the left. Same as your output without the index.

            – user10939484
            Jan 20 at 4:56






          • 1





            Thanks, I was not aware of that. Not bothering me. The assignment I'm working on has a finished dataframe image that does not have an index so it's been annoying me, making me think I was just missing something obvious.

            – user10939484
            Jan 20 at 5:15


















          0














          IIUC , you want the 1st row as headers, Use header=0:



          canada = pd.read_html(r'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M',header =0, flavor = 'bs4')


          Or:



          canada = pd.read_html(r'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M',header =0)

          cn_table=canada[0]
          >>cn_table

          Postcode Borough Neighbourhood
          0 M1A Not assigned Not assigned
          1 M2A Not assigned Not assigned
          2 M3A North York Parkwoods
          3 M4A North York Victoria Village
          4 M5A Downtown Toronto Harbourfront
          5 M5A Downtown Toronto Regent Park

          ... ... ... ...
          288 M9Z Not assigned Not assigned


          To save the dataframe to csv without index use:



          cn_table.to_csv('path+filename.csv',index=False)





          share|improve this answer


























          • The output still contains an index column.

            – user10939484
            Jan 20 at 4:38











          • @user10939484 can you share your expected output plz?

            – anky_91
            Jan 20 at 4:38











          • @user10939484 did you mean print(cn_table.to_string(index=False)) ??

            – anky_91
            Jan 20 at 4:45











          • No, I'd like to permanently modify the dataframe to output only the 3 columns without the index to the left. Same as your output without the index.

            – user10939484
            Jan 20 at 4:56






          • 1





            Thanks, I was not aware of that. Not bothering me. The assignment I'm working on has a finished dataframe image that does not have an index so it's been annoying me, making me think I was just missing something obvious.

            – user10939484
            Jan 20 at 5:15
















          0












          0








          0







          IIUC , you want the 1st row as headers, Use header=0:



          canada = pd.read_html(r'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M',header =0, flavor = 'bs4')


          Or:



          canada = pd.read_html(r'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M',header =0)

          cn_table=canada[0]
          >>cn_table

          Postcode Borough Neighbourhood
          0 M1A Not assigned Not assigned
          1 M2A Not assigned Not assigned
          2 M3A North York Parkwoods
          3 M4A North York Victoria Village
          4 M5A Downtown Toronto Harbourfront
          5 M5A Downtown Toronto Regent Park

          ... ... ... ...
          288 M9Z Not assigned Not assigned


          To save the dataframe to csv without index use:



          cn_table.to_csv('path+filename.csv',index=False)





          share|improve this answer















          IIUC , you want the 1st row as headers, Use header=0:



          canada = pd.read_html(r'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M',header =0, flavor = 'bs4')


          Or:



          canada = pd.read_html(r'https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M',header =0)

          cn_table=canada[0]
          >>cn_table

          Postcode Borough Neighbourhood
          0 M1A Not assigned Not assigned
          1 M2A Not assigned Not assigned
          2 M3A North York Parkwoods
          3 M4A North York Victoria Village
          4 M5A Downtown Toronto Harbourfront
          5 M5A Downtown Toronto Regent Park

          ... ... ... ...
          288 M9Z Not assigned Not assigned


          To save the dataframe to csv without index use:



          cn_table.to_csv('path+filename.csv',index=False)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 20 at 4:58

























          answered Jan 20 at 4:03









          anky_91anky_91

          4,3192319




          4,3192319













          • The output still contains an index column.

            – user10939484
            Jan 20 at 4:38











          • @user10939484 can you share your expected output plz?

            – anky_91
            Jan 20 at 4:38











          • @user10939484 did you mean print(cn_table.to_string(index=False)) ??

            – anky_91
            Jan 20 at 4:45











          • No, I'd like to permanently modify the dataframe to output only the 3 columns without the index to the left. Same as your output without the index.

            – user10939484
            Jan 20 at 4:56






          • 1





            Thanks, I was not aware of that. Not bothering me. The assignment I'm working on has a finished dataframe image that does not have an index so it's been annoying me, making me think I was just missing something obvious.

            – user10939484
            Jan 20 at 5:15





















          • The output still contains an index column.

            – user10939484
            Jan 20 at 4:38











          • @user10939484 can you share your expected output plz?

            – anky_91
            Jan 20 at 4:38











          • @user10939484 did you mean print(cn_table.to_string(index=False)) ??

            – anky_91
            Jan 20 at 4:45











          • No, I'd like to permanently modify the dataframe to output only the 3 columns without the index to the left. Same as your output without the index.

            – user10939484
            Jan 20 at 4:56






          • 1





            Thanks, I was not aware of that. Not bothering me. The assignment I'm working on has a finished dataframe image that does not have an index so it's been annoying me, making me think I was just missing something obvious.

            – user10939484
            Jan 20 at 5:15



















          The output still contains an index column.

          – user10939484
          Jan 20 at 4:38





          The output still contains an index column.

          – user10939484
          Jan 20 at 4:38













          @user10939484 can you share your expected output plz?

          – anky_91
          Jan 20 at 4:38





          @user10939484 can you share your expected output plz?

          – anky_91
          Jan 20 at 4:38













          @user10939484 did you mean print(cn_table.to_string(index=False)) ??

          – anky_91
          Jan 20 at 4:45





          @user10939484 did you mean print(cn_table.to_string(index=False)) ??

          – anky_91
          Jan 20 at 4:45













          No, I'd like to permanently modify the dataframe to output only the 3 columns without the index to the left. Same as your output without the index.

          – user10939484
          Jan 20 at 4:56





          No, I'd like to permanently modify the dataframe to output only the 3 columns without the index to the left. Same as your output without the index.

          – user10939484
          Jan 20 at 4:56




          1




          1





          Thanks, I was not aware of that. Not bothering me. The assignment I'm working on has a finished dataframe image that does not have an index so it's been annoying me, making me think I was just missing something obvious.

          – user10939484
          Jan 20 at 5:15







          Thanks, I was not aware of that. Not bothering me. The assignment I'm working on has a finished dataframe image that does not have an index so it's been annoying me, making me think I was just missing something obvious.

          – user10939484
          Jan 20 at 5:15




















          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%2f54273465%2fpandas-read-html-how-to-drop-index%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

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage