Using numpy to build an array of all combinations of two arrays

后端 未结 10 1317
温柔的废话
温柔的废话 2020-11-22 00:41

I\'m trying to run over the parameters space of a 6 parameter function to study it\'s numerical behavior before trying to do anything complex with it so I\'m searching for a

10条回答
  •  误落风尘
    2020-11-22 01:28

    Pandas merge offers a naive, fast solution to the problem:

    # given the lists
    x, y, z = [1, 2, 3], [4, 5], [6, 7]
    
    # get dfs with same, constant index 
    x = pd.DataFrame({'x': x}, index=np.repeat(0, len(x))
    y = pd.DataFrame({'y': y}, index=np.repeat(0, len(y))
    z = pd.DataFrame({'z': z}, index=np.repeat(0, len(z))
    
    # get all permutations stored in a new df
    df = pd.merge(x, pd.merge(y, z, left_index=True, righ_index=True),
                  left_index=True, right_index=True)
    

提交回复
热议问题