2
  1. I want to hide the index when pasting to this to sheet range

    sheet.range('B22').values = df
    
  2. How can I hide the column names (Data, Name)?

    print(df)
    
    Index Data  Name
    1      A     X
    2      B     Y
    3      C     Z
    4      D     L
    
martineau
  • 119,623
  • 25
  • 170
  • 301
Raj
  • 113
  • 1
  • 11
  • Note that you can control index/columns headers individually in xlwings like so: `sheet.range('B22').options(index=False, header=False).value = df`, see: http://docs.xlwings.org/en/stable/datastructures.html#pandas-dataframes – Felix Zumstein Oct 20 '18 at 10:09

2 Answers2

2

When writing a dataframe to Excel, you can do the following to hide both the column names and indices:

df.to_excel('test.xlsx', index=False, header=False)
rahlf23
  • 8,869
  • 4
  • 24
  • 54
  • 1
    I am using xlwings to achieve this. That is by place my data in specific range. How can i remove index from that range. – Raj Oct 13 '18 at 21:13
1

With xlwings use

import xlwings as xw
range1=df[['Data','Name']].values
wb = xw.Book('your_file.xlsx')
sht = wb.sheets['Sheet1']
sht.range('B22').value = range1

But, in general, using pd.to_excel can be easier. I think this is answered here [Python to_excel without row names (index)? and also here [Append existing excel sheet with new dataframe using python pandas

writer = pd.ExcelWriter('your_file.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False, startrow=21,startcol=1)

You can also add, 'header=None', to the to_excel function, if you want to drop 'Data' and 'Name'.

bart cubrich
  • 1,184
  • 1
  • 14
  • 41
  • Thank you, but I am trying to past the values in a specific range in an excel using xlwings. I then need to hide the indexes. Or it would be really helpful if we can just hide it from the dataframe – Raj Oct 13 '18 at 20:26
  • 1
    I edited to include and answer with xlwings. Consider adding xlwings in the description of your question. – bart cubrich Oct 13 '18 at 21:41