Python 数据帧列与自定义函数的成对关联
数据框上的熊猫在很多情况下都很方便。但是,在我的具体案例中,我希望使用Pandas(pearson、kendall或spearman除外)未提供的方法关联两列。是否可以明确定义在这种情况下使用的关联函数 我想要的语法如下所示:Python 数据帧列与自定义函数的成对关联,python,pandas,correlation,Python,Pandas,Correlation,数据框上的熊猫在很多情况下都很方便。但是,在我的具体案例中,我希望使用Pandas(pearson、kendall或spearman除外)未提供的方法关联两列。是否可以明确定义在这种情况下使用的关联函数 我想要的语法如下所示: def my_method(x,y): return something frame.corr(method=my_method) 对于任何类型的性能,您都需要在cython中执行此操作(使用cythonizable函数) 查看DataFrame.corr()的文档 还
def my_method(x,y): return something
frame.corr(method=my_method)
对于任何类型的性能,您都需要在cython中执行此操作(使用cythonizable函数)
查看DataFrame.corr()的文档 还可以查看DataFrame.corrwith()
警告:此方法计算对称相关矩阵,例如CramrsV,但此方法不适用于Thelsu和其他非对称corr矩阵。您能举例说明您的方法是什么吗?这并不重要。给定两个序列x和y,它返回[0,1]中的系数指出这两个变量之间的相关性,就像Spearman那样。不是问题的问题,而是在[-1, 1 ]中返回一个系数。除了杰夫所提到的Cython中,还可以考虑NUMPY或NUMBA的速度。
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)
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