python - 无法使corr工作

a_k*_*_ko 8 numpy correlation python-3.x pandas

我正在努力完成简单的关联.我尝试过类似问题所提出的所有建议.

以下是代码的相关部分,我所做的各种尝试及其结果.

import numpy as np
import pandas as pd

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

print (try01) 
Run Code Online (Sandbox Code Playgroud)

日期:

Empty DataFrame
Columns: []
Index: []
Run Code Online (Sandbox Code Playgroud)
try04 = data['ESA Index_close_px'][5:50].corr(data['CCMP Index_close_px'][5:50])
print (try04)
Run Code Online (Sandbox Code Playgroud)

日期:

**AttributeError: 'float' object has no attribute 'sqrt'**
Run Code Online (Sandbox Code Playgroud)

使用numpy

try05 = np.corrcoef(data['ESA Index_close_px'],data['CCMP Index_close_px'])
print (try05)
Run Code Online (Sandbox Code Playgroud)

日期:

AttributeError: 'float' object has no attribute 'sqrt'
Run Code Online (Sandbox Code Playgroud)

将列转换为列表

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)
Run Code Online (Sandbox Code Playgroud)

日期:

****TypeError: cannot perform reduce with flexible type****
Run Code Online (Sandbox Code Playgroud)

也试过.astype但没有任何区别.

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

data['CCMP Index_close_px'].astype(float)
Run Code Online (Sandbox Code Playgroud)

使用Python 3.5,pandas 0.18.1和numpy 1.11.1

非常感谢任何建议.

**edit1:*数据来自excel电子表格 data = pd.read_excel('C:\\Users\\Ako\\Desktop\\ako_files\\for_corr_??tool.xlsx') ,在关联尝试之前,只有列重命名和

data = data.drop(data.index[0]) 
Run Code Online (Sandbox Code Playgroud)

摆脱一条线

关于类型:

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



print (type (data['ESA Index_close_px'][1]))
Run Code Online (Sandbox Code Playgroud)

日期:

**edit2*部分数据:

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

print (data['CCMP Index_close_px'][1:10])
Run Code Online (Sandbox Code Playgroud)

日期:

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
Run Code Online (Sandbox Code Playgroud)

Yua*_*Tao 19

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

这对我很有用.希望它也可以帮到你.