How to create a pandas DataFrame from its indexes and a two variable function?
Question: This is a common pattern I've been using:

    
    
    rows = ['Joe','Amy','Tom']
    columns = ['account_no', 'balance']
    
    def f(row, column):
        '''Fetches value from database'''
        return np.random.random()
    
    pd.DataFrame([[f(row, column) for column in columns] for  row in rows], index=rows, columns=columns)
    

If the rows and columns are numerical, I can also use np.meshgrid:

    
    
    rows = [1,2,3]
    columns = [4,5]
    
    pd.DataFrame(np.vectorize(f)(xs, ys), index=rows, columns=columns)
    

My question is, what is the most elegant/Pythonic/"pandasic"/fastest/most
readable way to doing this in the general case?

Thanks!


Answer: a way of doing this could be to turn your function into a ufunc, and then use
[outer](http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.ufunc.outer.html)

    
    
    import numpy as np
    uf = np.frompyfunc(f, 2, 1) # f has 2 inputs, 1 outputs
    pd.DataFrame(uf.outer(rows, columns), index=rows, columns=columns)
    

one criterion you have above though is 'most readable' for which I'd say your
existing for loop solution is best.

