Search code examples
pythonpandasbroadcasting

Create a pandas dataframe with differences between two vectors of unequal length


I feel like I'm missing something obvious here.

I have two vectors of integers of different lengths (x and y). I'd like to create a pandas dataframe with x rows and y columns where each cell contains the difference between corresponding elements of the two vectors.

For example, given

v1 = np.array([2,4,8])
v2 = np.array([1,3])

v1 - v2

I'd like to get something back like

np.array([[ 1, -1],
          [ 3,  1],
          [ 7,  5]])

The code above is using numpy, but ultimately, I'd like to do this with Pandas.

Apologies in advance if this is a duplicate or unclear. I'm honestly not sure what terms to be searching for here.


Solution

  • To do this in NumPy, simply add an axis to one of the vectors:

    >>> v1[:, None] - v2
    array([[ 1, -1],
           [ 3,  1],
           [ 7,  5]])
    

    Then you could convert it into a Pandas dataframe, depending on your needs. Regarding Pandas, this answer about outer products (which is the equivalent operation for multiplication instead of subtraction) may be useful to you.