Python Forum
Removing the index column from df
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing the index column from df
#6
(Apr-05-2022, 11:55 AM)bnadir55 Wrote: what needs to be changed to accomplish these 2?
You most keep the original df object an not overwrite it.
Example make a own variable when convert to a string,i use perfringo code as example.
>>> df_str = df.to_string(index=False)
>>> print(df_str)
 a  b  c
 0  1  2
 3  4  5
 6  7  8
>>> 
>>> # Now still have the original object
>>> df
   a  b  c
0  0  1  2
1  3  4  5
2  6  7  8
>>> 
>>> type(df_str)
<class 'str'>
>>> type(df)
<class 'pandas.core.frame.DataFrame'>
So when have the original df object then it have a method .to_html()
Then the same there without index.
>>> print(df.to_html(index=False))
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>a</th>
      <th>b</th>
      <th>c</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>0</td>
      <td>1</td>
      <td>2</td>
    </tr>
    <tr>
      <td>3</td>
      <td>4</td>
      <td>5</td>
    </tr>
    <tr>
      <td>6</td>
      <td>7</td>
      <td>8</td>
    </tr>
  </tbody>
</table>  
Reply


Messages In This Thread
Removing the index column from df - by bnadir55 - Apr-05-2022, 07:03 AM
RE: Removing the index column from df - by bnadir55 - Apr-05-2022, 08:36 AM
RE: Removing the index column from df - by bnadir55 - Apr-05-2022, 11:55 AM
RE: Removing the index column from df - by snippsat - Apr-05-2022, 12:50 PM
RE: Removing the index column from df - by bnadir55 - Apr-05-2022, 01:27 PM
RE: Removing the index column from df - by snippsat - Apr-05-2022, 02:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pandas pivot table: How to find count for each group in Index and Column JaneTan 0 3,386 Oct-23-2021, 04:35 AM
Last Post: JaneTan
  Index error - columns vs non-column Vinny 3 4,986 Aug-09-2021, 04:46 PM
Last Post: snippsat
  How to define index column name using df.to_csv SriRajesh 0 1,793 Feb-13-2020, 03:45 PM
Last Post: SriRajesh
  How get row index of matching row in column 0 Sri 7 3,758 Apr-10-2019, 08:26 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020