Pandas DataFrame.corr(~) 方法计算源 DataFrame 中列的成对相关性。
注意
所有 NaN 值都将被忽略。
参数
1.method | string 或 callable | optional
要计算的相关系数的类型:
| 值 | 说明 | 
|---|---|
| 
 | 计算标准相关系数。 | 
| 
 | 计算 Kendall Tau 相关系数。 | 
| 
 | 计算斯皮尔曼等级相关性。 | 
| 
 | 该函数接受两个一维 Numpy 数组作为参数并返回单个浮点数。返回的矩阵始终是对称的,并且沿主对角线填充 1。 | 
默认情况下,method="pearson" 。
2. min_periods | int | optional
计算相关性所需的非NaN 值的最小数量。
返回值
DataFrame 表示源 DataFrame 中值的相关矩阵。
例子
基本用法
考虑以下 DataFrame :
df = pd.DataFrame({"A":[8,5,2,1],"B":[3,4,5,9]})
df
   A  B
0  8  3
1  5  4
2  2  5
3  1  9要计算两列的 "pearson" 相关性:
df.corr()
   A          B
A  1.000000   -0.841685
B  -0.841685  1.000000我们得到的结果是列 A 和 B 具有 -0.84 相关性。
指定min_periods
考虑以下 DataFrame :
df = pd.DataFrame({"A":[3,np.NaN,4],"B":[5,6,np.NaN]})
df
   A    B
0  3.0  5.0
1  NaN  6.0
2  4.0  NaN设置 min_periods=3 会产生:
df.corr(min_periods=3)
   A    B
A  NaN  NaN
B  NaN  NaN在这里,我们得到所有NaN的原因是,该方法忽略了NaN,因此每列只有2个值。由于我们已将计算相关性的最小阈值设置为 3 ,因此我们最终得到一个由 NaN 填充的 DataFrame 。
相关用法
- Python Pandas DataFrame corrwith方法用法及代码示例
- Python PySpark DataFrame corr方法用法及代码示例
- Python Pandas DataFrame copy方法用法及代码示例
- Python PySpark DataFrame collect方法用法及代码示例
- Python PySpark DataFrame coalesce方法用法及代码示例
- Python Pandas DataFrame convert_dtypes方法用法及代码示例
- Python Pandas DataFrame combine方法用法及代码示例
- Python Pandas DataFrame columns属性用法及代码示例
- Python PySpark DataFrame cov方法用法及代码示例
- Python Pandas DataFrame count方法用法及代码示例
- Python PySpark DataFrame colRegex方法用法及代码示例
- Python PySpark DataFrame columns属性用法及代码示例
- Python PySpark DataFrame count方法用法及代码示例
- Python Pandas DataFrame combine_first方法用法及代码示例
- Python Pandas DataFrame cov方法用法及代码示例
- Python Pandas DataFrame clip方法用法及代码示例
- Python Pandas DataFrame cummax方法用法及代码示例
- Python Pandas DataFrame cumprod方法用法及代码示例
- Python Pandas DataFrame cummin方法用法及代码示例
- Python Pandas DataFrame cumsum方法用法及代码示例
- Python Pandas DataFrame empty属性用法及代码示例
- Python Pandas DataFrame pop方法用法及代码示例
- Python Pandas DataFrame nsmallest方法用法及代码示例
- Python Pandas DataFrame sample方法用法及代码示例
- Python Pandas DataFrame items方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas DataFrame | corr method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
