11

在很多情况下,DataFrame 上的Pandas成对相关都很方便。但是,在我的具体情况下,我想使用 Pandas 未提供的方法(除了(pearson、kendall 或 spearman)之外的方法来关联两列。是否可以明确定义在这种情况下使用的关联函数?

我想要的语法如下所示:

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

2 回答 2

1

对于任何类型的性能,您都需要在 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)
于 2013-08-14T14:47:39.917 回答
0

查看 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

另请查看 DataFrame.corrwith()

警告:这会计算一个对称相关矩阵,例如。CramrsV,但这种方法不适用于 TheilsU 等非对称 corr 矩阵。

于 2019-12-29T18:30:49.753 回答