Python - 无法使corr工作

11

我正苦于完成一个简单的相关分析。我尝试了在类似问题下建议过的所有方法。

这里是代码的相关部分,我尝试过的各种方法以及它们的结果。

import numpy as np
import pandas as pd

try01 = data[['ESA Index_close_px', 'CCMP Index_close_px' ]].corr(method='pearson')

print (try01) 

输出:

Empty DataFrame
Columns: []
Index: []

try04 = data['ESA Index_close_px'][5:50].corr(data['CCMP Index_close_px'][5:50])
print (try04)

输出:

**AttributeError: 'float' object has no attribute 'sqrt'**

使用numpy

try05 = np.corrcoef(data['ESA Index_close_px'],data['CCMP Index_close_px'])
print (try05)

输出:

AttributeError: 'float' object has no attribute 'sqrt'

将列转换为列表

ESA_Index_close_px_list = list()
start_value = 1
end_value = len (data['ESA Index_close_px']) +1
for items in data['ESA Index_close_px']:
    ESA_Index_close_px_list.append(items)
    start_value = start_value+1    
    if start_value == end_value:
        break
    else:
        continue

CCMP_Index_close_px_list = list()
start_value = 1
end_value = len (data['CCMP Index_close_px']) +1
for items in data['CCMP Index_close_px']:
    CCMP_Index_close_px_list.append(items)
    start_value = start_value+1    
    if start_value == end_value:
        break
    else:
        continue

try06 = np.corrcoef(['ESA_Index_close_px_list','CCMP_Index_close_px_list'])
print (try06)

输出:

****TypeError: cannot perform reduce with flexible type****

同时尝试使用.astype,但没有任何区别。

data['ESA Index_close_px'].astype(float)

data['CCMP Index_close_px'].astype(float)

使用Python 3.5,pandas 0.18.1和numpy 1.11.1

非常感谢任何建议。

**编辑1:* 数据来自Excel电子表格 data = pd.read_excel('C:\\Users\\Ako\\Desktop\\ako_files\\for_corr_‌​tool.xlsx')在尝试相关性之前,只进行了列重命名和处理。

data = data.drop(data.index[0]) 

除去一条线:

关于类型:

print (type (data['ESA Index_close_px']))



print (type (data['ESA Index_close_px'][1]))

输出:

**编辑2* 数据的部分内容:

print (data['ESA Index_close_px'][1:10])

print (data['CCMP Index_close_px'][1:10])

输出:

2        2137
3        2138
4        2132
5        2123
6        2127
7     2126.25
8      2131.5
9      2134.5
10       2159
Name: ESA Index_close_px, dtype: object
2     5241.83
3     5246.41
4     5243.84
5     5199.82
6     5214.16
7     5213.33
8     5239.02
9     5246.79
10    5328.67
Name: CCMP Index_close_px, dtype: object

2
你能发布一些你的输入数据吗? - Maximilian Peters
我们需要看一下您是如何创建DataFrame“data”的。至少,我们需要了解更多关于它的信息,例如data.dtypes。我无法复制您在前三个示例中展示的内容。 - Warren Weckesser
当然: data = pd.read_excel('C:\\Users\\Ako\\Desktop\\ako_files\\for_corr_tool.xlsx') 这是来自Excel电子表格的数据。在进行相关性尝试之前,只有列重命名和 data = data.drop(data.index[0]) 以去除一行。关于类型: print (type (data['ESA Index_close_px'])) print (type (data['ESA Index_close_px'][1]))输出: <class 'pandas.core.series.Series'> <class 'float'> - a_ko
我们需要您编辑您的问题并直接复制一些数据。请查看如何创建良好的可重现Pandas示例 - IanS
谢谢Ian。我已经加入了更多的信息到问题中,并回复了Warren。当我执行 print (type (data ['ESA Index_close_px']))print (type (data ['ESA Index_close_px'] [1])) 时,输出结果是float。但是使用 print (data ['ESA Index_close_px'] [1:10]) 的时候,它显示 dtype object。 - a_ko
2个回答

25

今天我遇到了相同的问题。 尝试使用.astype('float64')来帮助使类型正确。
data['ESA Index_close_px'][5:50].astype('float64').corr(data['CCMP Index_close_px'][5:50].astype('float64'))

这对我很有效,希望它也可以帮助你。


0

您可以尝试以下方法:

Top15['Citable docs per capita']=(Top15['Citable docs per capita']*100000)
Top15['Citable docs per capita'].astype('int').corr(Top15['Energy Supply per Capita'].astype('int'))

这对我有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接