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!

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)
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) :
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)
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?
(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>
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
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()