pandas python - 无法使 corr 工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40453337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python - cannot make corr work
提问by a_ko
I'm struggling with getting a simple correlation done. I've tried all that was suggested under similar questions.
我正在努力完成一个简单的相关性。我已经尝试了在类似问题下建议的所有内容。
Here are the relevant parts of the code, the various attempts I've made and their results.
以下是代码的相关部分、我所做的各种尝试及其结果。
import numpy as np
import pandas as pd
try01 = data[['ESA Index_close_px', 'CCMP Index_close_px' ]].corr(method='pearson')
print (try01)
Out:
出去:
Empty DataFrame
Columns: []
Index: []
try04 = data['ESA Index_close_px'][5:50].corr(data['CCMP Index_close_px'][5:50])
print (try04)
Out:
出去:
**AttributeError: 'float' object has no attribute 'sqrt'**
using numpy
使用 numpy
try05 = np.corrcoef(data['ESA Index_close_px'],data['CCMP Index_close_px'])
print (try05)
Out:
出去:
AttributeError: 'float' object has no attribute 'sqrt'
converting the columns to lists
将列转换为列表
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)
Out:
出去:
****TypeError: cannot perform reduce with flexible type****
Also tried .astype but not made any difference.
也试过 .astype 但没有任何区别。
data['ESA Index_close_px'].astype(float)
data['CCMP Index_close_px'].astype(float)
Using Python 3.5, pandas 0.18.1 and numpy 1.11.1
使用 Python 3.5、pandas 0.18.1 和 numpy 1.11.1
Would really appreciate any suggestion.
真的很感激任何建议。
**edit1:*
Data is coming from an excel spreadsheet
data = pd.read_excel('C:\\Users\\Ako\\Desktop\\ako_files\\for_corr_??tool.xlsx')
prior to the correlation attempts, there are only column renames and
**edit1:* 数据来自data = pd.read_excel('C:\\Users\\Ako\\Desktop\\ako_files\\for_corr_??tool.xlsx')
尝试关联之前的 Excel 电子表格
,只有列重命名和
data = data.drop(data.index[0])
to get rid of a line
摆脱一条线
regarding the types:
关于类型:
print (type (data['ESA Index_close_px']))
print (type (data['ESA Index_close_px'][1]))
Out:
出去:
**edit2* parts of the data:
**edit2* 部分数据:
print (data['ESA Index_close_px'][1:10])
print (data['CCMP Index_close_px'][1:10])
Out:
出去:
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
回答by Yuan Tao
Well, I've encountered the same problem today.
try use .astype('float64')
to help make the type correct.data['ESA Index_close_px'][5:50].astype('float64').corr(data['CCMP Index_close_px'][5:50].astype('float64'))
好吧,我今天也遇到了同样的问题。尝试使用.astype('float64')
帮助使类型正确。data['ESA Index_close_px'][5:50].astype('float64').corr(data['CCMP Index_close_px'][5:50].astype('float64'))
This works well for me. Hope it can help you as well.
这对我很有效。希望它也能帮到你。
回答by Paridhi Asthana
You can try as following:
您可以尝试如下:
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'))
It worked for me.
它对我有用。