[Python] How to list a 2d array in a tabular form along with two 1d arrays from which it was...

Discussão em 'Python' iniciado por Stack, Março 17, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to calculate a 2d variable z = x + y where x and y are 1d arrays of unequal dimensions (say, x- and y-coordinate points on a spatial grid). I'd like to display the result row-by-row in which the values of x and y are in the first two columns and the corresponding value of z calculated from these x and y values are in the third, something like the following for x = [1, 2] and y = [3, 4, 5]:

    x y z
    1 3 4
    1 4 5
    1 5 6
    2 3 5
    2 4 6
    2 5 7


    The code below works (using lists here, but will probably need numpy arrays later):

    import pandas as pd

    x = [1, 2]
    y = [3, 4, 5]
    col1 = []
    col2 = []
    z = []
    for i in range(len(x)):
    for j in range(len(y)):
    col1.append(x)
    col2.append(y[j])
    z.append(x+y[j])

    df = pd.DataFrame(zip(col1, col2, z), columns=["x", "y", "z"])
    print(df)


    Just wondering, is there a better way of doing this without using the loop by some combination of meshgrid, indices, flatten, v/hstack, and reshape? The size of x and y will typically be around 100.

    Continue reading...

Compartilhe esta Página