pandas Python熊猫空相关矩阵
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/22481271/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python pandas empty correlation matrix
提问by Max
I am running Python 2.7.6, pandas 0.13.1. I am unable to compute a correlation matrix from a DataFrame, and I'm not sure why. Here is my example DataFrame:
我正在运行 Python 2.7.6,pandas 0.13.1。我无法从 DataFrame 计算相关矩阵,我不确定为什么。这是我的示例数据帧:
In [24]: foo
Out[24]:
                       A             B            C
2011-10-12   0.006204908 -0.0009503677  0.003480105
2011-10-13    0.00234903 -0.0005122284 -0.001738786
2011-10-14    0.01045599   0.000346268  0.002378351
2011-10-17   0.003239088   0.001246239 -0.002651856
2011-10-18   0.001717674 -0.0001738079  0.002013923
2011-10-19  0.0001919342  6.399505e-05 -0.001311259
2011-10-20  0.0007430615   0.001186141  0.001919222
2011-10-21   -0.01075129    -0.0015123  0.000807017
2011-10-24   -0.00819597 -0.0005124197  0.003037654
2011-10-25   -0.01604287   0.001157013 -0.001227516
[10 rows x 3 columns]
Now I'll try to compute the correlation:
现在我将尝试计算相关性:
In [27]: foo.corr()
Out[27]:
Empty DataFrame
Columns: []
Index: []
[0 rows x 0 columns]
On the other hand, I can compute correlations of each column to each other column. For example:
另一方面,我可以计算每列与其他列的相关性。例如:
In [31]: foo['A'].corr(foo['B'])
Out[31]: 0.048578514633405255
Any idea what might be causing this issue? Thanks a lot.
知道什么可能导致这个问题吗?非常感谢。
Version Info
版本信息
In [34]: import pandas as pd
In [35]: pd.__version__
Out[35]: '0.13.1'
回答by Max
As Jeff mentioned in the comments, the problem resulted from my columns having the objectdtype. For future reference, even if the object looks numeric, check the dtype and make sure it is numeric (e.g. do foo.astype(float)) before computing the correlation matrix.
正如杰夫在评论中提到的,问题是由于我的列具有objectdtype。为了将来参考,即使对象看起来是数字,foo.astype(float)在计算相关矩阵之前检查 dtype 并确保它是数字(例如 do )。

