pandas.DataFrame.corrwith#
- DataFrame.corrwith(other, axis=0, drop=False, method='pearson', numeric_only=False, min_periods=None)[源代码][源代码]#
计算成对相关性。
成对相关性是在 DataFrame 的行或列与 Series 或 DataFrame 的行或列之间计算的。在计算相关性之前,DataFrame 首先沿着两个轴对齐。
- 参数:
- 其他DataFrame, Series
用于计算相关性的对象。
- 轴{0 或 ‘index’, 1 或 ‘columns’},默认 0
要使用的轴。0 或 ‘index’ 用于按行计算,1 或 ‘columns’ 用于按列计算。
- 删除bool, 默认为 False
从结果中删除缺失的索引。
- 方法{‘pearson’, ‘kendall’, ‘spearman’} 或可调用
相关方法:
pearson : 标准相关系数
kendall : Kendall Tau 相关系数
spearman : Spearman 等级相关
- callable: 带有两个一维 ndarrays 输入的可调用对象
并返回一个浮点数。
- numeric_onlybool, 默认为 False
只包含 float, int 或 boolean 数据。
- min_periodsint, 可选
需要的最小观测数以获得有效结果。
Added in version 1.5.0.
在 2.0.0 版本发生变更:
numeric_only的默认值现在是False。
- 返回:
- 系列
成对相关性。
参见
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