pandas.core.groupby.DataFrameGroupBy.corrwith#

DataFrameGroupBy.corrwith(other, axis=_NoDefault.no_default, drop=False, method='pearson', numeric_only=False)[源代码]#

计算成对相关性。

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

Parameters:
otherDataFrame, Series

用于计算相关性的对象。

axis{0 或 ‘index’, 1 或 ‘columns’}, default 0

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

dropbool,默认 False

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

<strong>method</strong>{‘pearson’, ‘kendall’, ‘spearman’} 或 callable

相关性方法:

  • pearson : 标准相关系数

  • kendall : Kendall Tau 相关系数

  • spearman : Spearman 秩相关系数

  • callable: callable,接收两个一维 ndarray 作为输入

    并返回一个 float。

numeric_onlybool,默认 False

仅包含 float, intboolean 数据。

在 1.5.0 版本加入.

在 2.0.0 版本发生变更: numeric_only 的默认值现在是 False

Returns:
Series

成对相关性。

参见

DataFrame.corr

计算列的成对相关性。

Examples

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