簡體   English   中英

Pandas DataFrame 列與自定義函數的成對關聯

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

在許多情況下,DataFrame 上的 Pandas 成對相關很方便。 但是,在我的特定情況下,我想使用 Pandas 未提供的方法(除 (pearson、kendall 或 spearman) 之外的其他方法)來關聯兩列。是否可以明確定義在這種情況下使用的相關函數?

我想要的語法如下所示:

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

對於任何類型的性能,您都需要在 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)

查看 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矩陣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM