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

MJS picture MJS · Sep 11, 2015 · Viewed 8.1k times · Source
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:

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

Thanks in advance for your help.

Answer

zero323 picture zero323 · Sep 11, 2015

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

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