简体   繁体   中英

how do I disply an np.array in column with xlwings

I have the following code in python with xlwings

@xlfunc
@xlarg('x', 'nparray', ndim=1)
@xlarg('y', 'nparray', ndim=1)
def test_sum(x,y):
    return(x+y)

Once in excel, I submit ctrl+shift+Enter but it displays the result in a line and not in column. How can I correct this ?

Since you are forcing your inputs to be 1-dimensional numpy arrays of the form np.array([1, 2, 3]) , the result is a 1-dim numpy array as well which are (like simple lists) interpreted as row orientation when written to Excel. To write an array in column orientation, you'll need to return two dimensional arrays of the form np.array([[1], [2], [3]]) or for lists: [[1], [2], [3]] .

In your case, you could simply change ndim=1 to ndim=2 and it will be returned in column orientation if the inputs are columns as well. If you would like to keep the inputs in 1d (for whatever reason), then you could force the output into column orientation eg by doing this:

from xlwings import xlfunc, xlarg
import numpy as np

@xlfunc
@xlarg('x', 'nparray', ndim=1)
@xlarg('y', 'nparray', ndim=1)
def test_sum(x,y):
    return(x+y)[:, np.newaxis]

You're right that the docs aren't clear enough about this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM