I created a simple DataFrame using Pandas, and I need to add/replace it to Sheet2 in my Xlsx File. Sheets in Xlsx files are: Sheet1, Sheet2 I have two problems:
First being that even tho I specify the sheetName, it all it does is removes all sheets that were already in the database, and creates only one with the name specified with the data.
Second is I can't remove the column index from the dataframe. I tried adding 'index_col=None' but all it does is causes errors.
Code So Far:
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import numpy as np
Number1 = '123456'
Number2 = '987654'
Number3 = '888888'
Comp = 'HelloAll'
excel_file = 'C:/Users/username/Desktop/testFile.xlsx'
data = {'Number1': [Number1],
'Number2': [Number2],
'Number3': [Number3],
'Comp': [Comp]
}
df = pd.DataFrame (data, columns = ['Number1','Number2', 'Number3', 'Comp'])
pd.set_option('display.max_columns', None)
print (df)
with ExcelWriter(excel_file) as writer:
df.to_excel(writer, sheet_name='Sheet2')
Expected Result:
Number1 Number2 Number3 Comp
123456 987654 888888 HelloAll