【问题标题】:python - cannot make corr workpython - 无法使 corr 工作
【发布时间】:2017-03-20 02:20:27
【问题描述】:

我正在努力完成一个简单的关联。我已经尝试了类似问题下的所有建议。

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

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

【问题讨论】:

  • 你能发布一些你的输入数据吗?
  • 我们需要看看你是如何创建DataFrame data的。至少,我们需要更多地了解它,比如data.dtypes。我无法重现您在前三个示例中展示的内容。
  • 当然: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'])) @987654341 @ out:
  • 我们需要您编辑您的问题并直接在那里复制一些数据。请查看How to make good reproducible pandas examples
  • 感谢 Ian 我用附加信息编辑了 Q。也添加到我对沃伦的回应中。当我这样做时: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

标签: python-3.x pandas numpy correlation


【解决方案1】:

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

这对我很有效。希望对你也有帮助。

【讨论】:

    【解决方案2】:

    你可以尝试如下:

    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'))
    

    它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 2021-09-10
      • 1970-01-01
      • 2017-05-30
      • 2016-08-22
      相关资源
      最近更新 更多