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?
Answers:
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 = ...
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?
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 = ...