one = pd.DataFrame(data=[1,2,3,4,5], index=[1,2,3,4,5])
two = pd.DataFrame(data=[5,4,3,2,1], index=[1,2,3,4,5])
one.corr(two)
我认为它应该返回一个 float = -1.00 但它会产生以下错误:
TypeError:无法将 ['pearson'] 与块值进行比较
在此先感谢您的帮助。
one = pd.DataFrame(data=[1,2,3,4,5], index=[1,2,3,4,5])
two = pd.DataFrame(data=[5,4,3,2,1], index=[1,2,3,4,5])
one.corr(two)
我认为它应该返回一个 float = -1.00 但它会产生以下错误:
TypeError:无法将 ['pearson'] 与块值进行比较
在此先感谢您的帮助。
pandas.DataFrame.corr
计算单个数据帧的列之间的成对相关性。你需要的是pandas.DataFrame.corrwith
:
>>> one.corrwith(two)
0 -1
dtype: float64
DataFrame
当您应该对 a 进行操作时,您正在对 a进行操作Series
。
In [1]: import pandas as pd
In [2]: one = pd.DataFrame(data=[1,2,3,4,5], index=[1,2,3,4,5])
In [3]: two = pd.DataFrame(data=[5,4,3,2,1], index=[1,2,3,4,5])
In [4]: one
Out[4]:
0
1 1
2 2
3 3
4 4
5 5
In [5]: two
Out[5]:
0
1 5
2 4
3 3
4 2
5 1
In [6]: one[0].corr(two[0])
Out[6]: -1.0
为什么用下标[0]
?因为那是 中列的名称DataFrame
,因为您没有给它一个。当您引用 a 中的列时DataFrame
,它将返回 a Series
,它是一维的。这个函数的文档在这里。