Pandas DataFrame.cov(~)方法计算源 DataFrame 中列的协方差矩阵。请注意,协方差的无偏估计量用来:
其中,
-
是列中值的数量
-
是 列的样本平均值
-
是 列的样本平均值
-
和分别是和列中的第值。
注意
所有 NaN 值都将被忽略。
参数
1.min_periods | int | optional
计算协方差的非 NaN 值的最小数量。
返回值
DataFrame 表示源 DataFrame 中值的协方差矩阵。
例子
基本用法
考虑以下 DataFrame :
df = pd.DataFrame({"A":[2,4,6],"B":[3,4,5]})
df
A B
0 2 3
1 4 4
2 6 5
要计算两列的协方差:
df.cov()
A B
A 4.0 2.0
B 2.0 1.0
在这里,我们得到以下结果:
-
列
A和B的样本协方差为2.0。 -
A列的样本方差为4.0,B列的样本方差为1.0。
指定min_periods
考虑以下带有一些缺失值的DataFrame:
df = pd.DataFrame({"A":[3,np.NaN,4],"B":[5,6,7]})
df
A B
0 3.0 5.0
1 NaN 6.0
2 4.0 7.0
设置 min_periods=3 会产生:
df.cov(min_periods=3)
A B
A NaN NaN
B NaN 1.0
在这里,我们得到 NaN 的原因是,由于该方法忽略了 NaN ,因此 A 列只有 2 个值。由于我们已将计算协方差的最小阈值设置为 3 ,因此我们最终得到一个由 NaN 填充的 DataFrame 。
相关用法
- Python PySpark DataFrame cov方法用法及代码示例
- Python Pandas DataFrame copy方法用法及代码示例
- Python PySpark DataFrame collect方法用法及代码示例
- Python PySpark DataFrame coalesce方法用法及代码示例
- Python Pandas DataFrame corrwith方法用法及代码示例
- Python PySpark DataFrame corr方法用法及代码示例
- Python Pandas DataFrame convert_dtypes方法用法及代码示例
- Python Pandas DataFrame combine方法用法及代码示例
- Python Pandas DataFrame columns属性用法及代码示例
- Python Pandas DataFrame count方法用法及代码示例
- Python PySpark DataFrame colRegex方法用法及代码示例
- Python PySpark DataFrame columns属性用法及代码示例
- Python PySpark DataFrame count方法用法及代码示例
- Python Pandas DataFrame corr方法用法及代码示例
- Python Pandas DataFrame combine_first方法用法及代码示例
- 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 | cov method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
