Remove Automatically Generated Number Column in Dataframe Pandas

Asked

Viewed 1,805 times

0

I created a dataframe in pandas, everything works perfect, but when I export to Excel, it appears this first column with these numbers. I would like to remove it, but commands like . drop do not work.resultado no excel

df = {}
item = []
count = []
def dataframe(df):
    for i in ranking:
        item.append(i['item'])
        count.append(i['count'])
    df = pd.DataFrame(df, columns=('Item','Count'))
    df['Item'] = item
    df['Count'] = count
    df.sort_values('Count', axis=0, ascending = False, inplace=True)
    df.to_excel('C:/hashtag_teste.xlsx')
    print(df)
    return df


1 answer

4

By default, DataFrame.to_excel saves the index of your Dataframe to the Excel table. The index of a Dataframe (also by default) is a numerical sequence starting from 0.

It is easy to change this behavior while saving the Excel file. Simply pass the argument index=False:

df.to_excel('C:/hashtag_teste.xlsx', index=False)
  • I thought I tried this, but maybe I was putting it in the wrong place. I’m still learning. Thank you so much for your help :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.