From pandas to excel via xlwings – do not deposit index

Question:

Range("A1").value = p.df_sector[["A","B","C"]].sort(columns=["C"],ascending=False).head(4)

Works wonderfully! But – I do not want/need to see the index column

p.df_sector[["A","B","C"]].sort(columns=["C"],ascending=False).head(4).to_string(index=False)

Would do what I need, but then the data deposits in a single cell!

.to_matrix()

Does what I need but then I lose my headers (which I require).

Any input on how to dump the df with headers and without index?

Asked By: jason m

||

Answers:

There’s an example in the docs about working with pandas. Also check the docs about Range. In your case:

sht.range("A1").options(index=False).value = p.df_sector[["A","B","C"]].sort(columns=["C"],ascending=False).head(4)
Answered By: Felix Zumstein

The documentation and syntax seems to have changed a bit, since 2015.

Here is the documentation for dealing with Pandas via xlwings.

Instead of a parameter in Range, index=False needs to be inside a .options. So if you’re dealing with worksheet sht in your code, the left-hand side of your equation should be

sht.range('A1').options(index=True).value = ...
Answered By: lebelinoz
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.