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
#1
Hello all,
I have a data frame that I pull from excel using: df = pd.read_excel(file.xlsx)
when I print results it returns with the index column on the left (0,1,2,3,4,...)
no matter what I tried, including df.reset_index(drop=True), does not remove the index column on print(df)
can someone please share what can be done to exclude the index column?

your help is highly appreciated! Smile
Reply
#2
Are you sure it doesn't work? df.reset_index(drop=True) does not work in place. It creates a new Dataframe that is the return value of the function call. Are you doing this:
new_df = df.reset_index(drop=True)
Reply
#3
Yes, also created now a whole new df :

new_df = df.reset_index(drop=True)

print(new_df)
and still got the index # (see attached) :

Attached Files

Thumbnail(s)
   
Reply
#4
Documentation (pandas.DataFrame.reset_index) states:

Quote:Reset the index of the DataFrame, and use the default one instead. If the DataFrame has a MultiIndex, this method can remove one or more levels.

As name suggest, it's resetting and not deleting (unless df has MultiIndex, in which case some but not all levels could be removed). drop parameter is used to avoid the old index being added as a column (with no drop=True old index will be added as column to dataframe).

If you need to output the dataframe without index then you can do something like this:

>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame(np.arange(9).reshape(3,3), columns=list('abc'))
>>> df
   a  b  c
0  0  1  2
1  3  4  5
2  6  7  8
>>> print(df.to_string(index=False))
 a  b  c
 0  1  2
 3  4  5
 6  7  8
As name suggests this is actually string, not dataframe what will be printed.

If you need to write back to excel without index then something like df.to_excel(index=False) should work (be vary in case of MultiIndex)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Thx! changed as you sugested to this : df = df.to_string(index=False)
but later on in the script I'm sending mail using this :
mail.HTMLBody = df.to_html()

and checking if df is empty :
if not df.empty:
mail.Send()


and for both I get error :
'str' object has no attribute 'to_html'
'str' object has no attribute 'empty'

what needs to be changed to accomplish these 2?
Reply
#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
#7
Thx! if I got you right,
I need to use the nonindexed df to be used in the mail sent and the empty check, what you show is using the non-index df for the print and the indexed df for the mail and empty check, but I need the non-index to be used in the mail and empty as well
Reply
#8
You do all check on the original df(DataFrame),df.to_string and df.to_html is kind of last step when finish doing operation or check of the DataFrame.
if not df.empty:
    mail.HTMLBody = df.to_html(index=False)
    mail.Send()
Reply


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,297 Oct-23-2021, 04:35 AM
Last Post: JaneTan
  Index error - columns vs non-column Vinny 3 4,913 Aug-09-2021, 04:46 PM
Last Post: snippsat
  How to define index column name using df.to_csv SriRajesh 0 1,763 Feb-13-2020, 03:45 PM
Last Post: SriRajesh
  How get row index of matching row in column 0 Sri 7 3,639 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