简体   繁体   English

Pandas DataFrame 列与自定义函数的成对关联

[英]Pairwise correlation of Pandas DataFrame columns with custom function

Pandas pairwise correlation on a DataFrame comes handy in many cases.在许多情况下,DataFrame 上的 Pandas 成对相关很方便。 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?但是,在我的特定情况下,我想使用 Pandas 未提供的方法(除 (pearson、kendall 或 spearman) 之外的其他方法)来关联两列。是否可以明确定义在这种情况下使用的相关函数?

The syntax I would like looks like this:我想要的语法如下所示:

def my_method(x,y): return something
frame.corr(method=my_method)

You would need to do this in cython for any kind of perf (with a cythonizable function)对于任何类型的性能,您都需要在 cython 中执行此操作(使用 cythonizable 功能)

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)

Check out the documentation for DataFrame.corr()查看 DataFrame.corr() 的文档

Parameters
----------
    method : {'pearson', 'kendall', 'spearman'} or callable
        * pearson : standard correlation coefficient
        * kendall : Kendall Tau correlation coefficient
        * spearman : Spearman rank correlation
        * callable: callable with input two 1d ndarrays
            and returning a float. Note that the returned matrix from corr
            will have 1 along the diagonals and will be symmetric
            regardless of the callable's behavior
            .. versionadded:: 0.24.0

Check out also DataFrame.corrwith()另请查看 DataFrame.corrwith()

Warning : This calculates a symmetric correlation matrix, eg.警告:这将计算对称相关矩阵,例如。 CramrsV, but this method is not suitable for TheilsU and other asymmetric corr matrix. CramrsV,但这种方法不适用于TheilsU等非对称corr矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM