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

2024-09-27 00:23:23 发布

您现在位置:Python中文网/ 问答频道 /正文

在许多情况下,数据帧上的Pandaspairwise correlation非常方便。然而,在我的具体案例中,我希望使用Pandas没有提供的方法(pearson、kendall或spearman)来关联两个列。在这种情况下,是否可以明确定义要使用的相关函数?在

我想要的语法如下:

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

Tags: 数据方法函数pandas定义my语法情况
2条回答

对于任何类型的perf(使用cythonizable函数)都需要在cython中执行此操作

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)

查看的文档数据帧.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

也可以查看数据帧.corrwith()

警告:此方法计算对称相关矩阵,如CramrsV,但此方法不适用于TheilsU和其他非对称corr矩阵。在

相关问题 更多 >

    热门问题