pandas Python 熊猫相关 corr() TypeError:无法将 ['pearson'] 与块值进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32529217/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:52:30  来源:igfitidea点击:

Python pandas correlation corr() TypeError: Could not compare ['pearson'] with block values

pythonpandastypeerrorcorrelationpearson

提问by MJS

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)

I think it should return a float = -1.00 but instead it's generating the following error:

我认为它应该返回一个 float = -1.00 但它产生了以下错误:

TypeError: Could not compare ['pearson'] with block values

类型错误:无法将 ['pearson'] 与块值进行比较

Thanks in advance for your help.

在此先感谢您的帮助。

回答by zero323

pandas.DataFrame.corrcomputes pairwise correlation between the columns of a single data frame. What you need here is pandas.DataFrame.corrwith:

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

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

回答by Brian

You are operating on a DataFramewhen you should be operating on a Series.

您在操作DataFrame时,你应该对进行操作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

Why subscript with [0]? Because that is the name of the column in the DataFrame, since you didn't give it one. When you reference a column in a DataFrame, it will return a Series, which is 1-dimensional. The documentation for this function is here.

为什么用 下标[0]?因为那是 中列的名称DataFrame,因为你没有给它一个。当您引用 a 中的列时DataFrame,它将返回 a Series,它是一维的。此功能的文档在此处