我正在努力完成简单的关联。 我尝试过类似问题所提出的所有建议。
以下是代码的相关部分,我所做的各种尝试及其结果。
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
非常感谢任何建议。
** EDIT1:*
数据来自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]))
日期:
*** EDIT2
部分数据:
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'])) print (type (data['ESA Index_close_px'][1])) out:
我们需要您编辑您的问题并直接复制一些数据。 请查看如何制作好的可重复的熊猫示例。
谢谢Ian我用附加信息编辑了Q. 还添加到我对沃伦的回复中。 当我这样做:print (type (data['ESA Index_close_px']))和print (type (data['ESA Index_close_px'][1]))我得到浮动但是用print (data['ESA Index_close_px'][1:10])它说dtype对象
好吧,我今天遇到了同样的问题。
尝试使用.astype('float64')来帮助使类型正确。
data['ESA Index_close_px'][5:50].astype('float64').corr(data['CCMP Index_close_px'][5:50].astype('float64'))
这对我很有用。 希望它也可以帮到你。
你可以尝试如下:
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'))
它对我有用。