Pairwise correlation of Pandas DataFrame columns with custom function

Flo*_*lai 5 python correlation pandas

Pandas pairwise correlation on a DataFrame comes handy in many cases. However, in my specific case I would like to use a method not provided by Pandas (something other than (pearson, kendall or spearman) to correlate two columns. Is it possible to explicitly define the correlation function to use in this case?

The syntax I would like looks like this:

def my_method(x,y): return something
frame.corr(method=my_method)
Run Code Online (Sandbox Code Playgroud)

Jef*_*eff 2

对于任何类型的性能,您都需要在 cython 中执行此操作(使用 cythonized 函数)

l = len(df.columns)
results = np.zeros((l,l))
for i, ac in enumerate(df):
    for j, bc in enumerate(df):
           results[j,i] = func(ac,bc)
results = DataFrame(results,index=df.columns,columns=df.columns)
Run Code Online (Sandbox Code Playgroud)