Krishnamurthy
Krishnamurthy

Reputation: 139

Removing "row index" in pandas

I have the below dataframe wherein I am trying to remove the first column which is the default column assigned in pandas dataframe/series. How do I get rid of that?

Actual Result

Actual result should be without the first column which has the "0". I have tried using reset index with drop but nothing works.

Here is my code which sends a mail with the dataframe output.

def send_email(index):
    fromaddr = ""
    toaddr = ""
    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = "Timestamp Alarm"
    df1 = df.iloc[index,1:]
    new = df1

    body = """\
<html>
  <head></head>
  <body>
    {0}
  </body>
</html>
""".format(pd.DataFrame(new).T.to_html())
    part1 = MIMEText(body,'html')
    msg.attach(part1)
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()
    server.login('***', '**')
    server.sendmail('','',msg.as_string())
    server.quit()

Upvotes: 1

Views: 7260

Answers (2)

Valdi_Bo
Valdi_Bo

Reputation: 30991

Look at to_html documentation.

This fuction contains a.o. index parameter (default True), deciding whether to generate the index column.

Maybe you should pass it with False value.

Another option:

Print the DataFrame (and then send) as "ordinary" text.

To hide the index column, also pass index=False:

print(df.to_string(index=False))

Upvotes: 1

CypherX
CypherX

Reputation: 7353

See the following and relate to your problem.

Generic Solution

import pandas as pd
d = {'Field_A': 'FA', 'Field_B': 'FB', 'Field_C': 'FC'}
df = pd.DataFrame({0: d}).T
df

Output:

    Field_A Field_B Field_C
0   FA      FB      FC

Specific Solution

If all you want is the cell values for columns Field_A, Field_B, Field_C, then just call df.loc[0]. In your case, you would need to change the line where you define variable new.

new = df.loc[index]

Upvotes: 0

Related Questions