pandas.DataFrame.corrwith#

DataFrame.corrwith(other, axis=0, drop=False, method='pearson', numeric_only=False, min_periods=None)[源码]#

计算成对相关性。

计算 DataFrame 的行或列与 Series 或 DataFrame 的行或列之间的成对相关性。在计算相关性之前,DataFrame 会首先沿两个轴对齐。

参数:
otherDataFrame, Series

用于计算相关性的对象。

axis{0 或 ‘index’, 1 或 ‘columns’}, 默认为 0

使用的轴。0 或 ‘index’ 表示按行计算,1 或 ‘columns’ 表示按列计算。

dropbool, default False

从结果中删除缺失的索引。

method{‘pearson’, ‘kendall’, ‘spearman’} 或可调用对象

相关性方法

  • pearson : 标准相关系数

  • kendall : Kendall Tau 相关系数

  • spearman : Spearman 秩相关

  • callable: 接收两个一维 ndarray 并返回一个浮点数的函数

    并返回一个浮点数。

numeric_onlybool, default False

仅包含 floatintboolean 数据。

min_periodsint, optional

要有有效结果所需的最小观测次数。

版本 2.0.0 已更改: numeric_only 的默认值现在是 False

返回:
Series

成对相关性。

另请参阅

DataFrame.corr

计算列的成对相关性。

示例

>>> index = ["a", "b", "c", "d", "e"]
>>> columns = ["one", "two", "three", "four"]
>>> df1 = pd.DataFrame(
...     np.arange(20).reshape(5, 4), index=index, columns=columns
... )
>>> df2 = pd.DataFrame(
...     np.arange(16).reshape(4, 4), index=index[:4], columns=columns
... )
>>> df1.corrwith(df2)
one      1.0
two      1.0
three    1.0
four     1.0
dtype: float64
>>> df2.corrwith(df1, axis=1)
a    1.0
b    1.0
c    1.0
d    1.0
e    NaN
dtype: float64