Skip to main content
 首页 » 编程设计

Python pandas correlation corr() 类型错误 : Could not compare ['pearson' ] with block values

2025年05月04日8qq78292959

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: Could not compare ['pearson'] with block values

预先感谢您的帮助。

请您参考如下方法:

pandas.DataFrame.corr 计算单个数据帧的列之间的成对相关性。这里需要的是 pandas.DataFrame.corrwith :

>>> one.corrwith(two) 
0   -1 
dtype: float64