Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From pandas to excel via xlwings - do not deposit index

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?

like image 220
jason m Avatar asked Dec 18 '22 23:12

jason m


2 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 = ...
like image 195
lebelinoz Avatar answered Jan 12 '23 19:01

lebelinoz


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)
like image 24
Felix Zumstein Avatar answered Jan 12 '23 19:01

Felix Zumstein