本文介绍了Python Pandas空相关矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Python 2.7.6,pandas 0.13.1.我无法从DataFrame计算相关矩阵,也不确定为什么.这是我的示例DataFrame:

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:

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.

版本信息

In [34]: import pandas as pd

In [35]: pd.__version__
Out[35]: '0.13.1'

推荐答案

正如Jeff在评论中所提到的,问题是由于我的列具有object dtype而引起的.为了将来参考,即使对象看起来是数字,在计算相关矩阵之前,请检查dtype并确保它是数字(例如,执行foo.astype(float)).

As Jeff mentioned in the comments, the problem resulted from my columns having the object dtype. 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.

这篇关于Python Pandas空相关矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:03