pandas 类型错误:corr() 缺少 1 个必需的位置参数:“其他”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44381891/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:44:24  来源:igfitidea点击:

TypeError: corr() missing 1 required positional argument: 'other'

pythonpandasnumpy

提问by Andrey Stebenkov

I am new to python and have hit a wall. I should count pearson correlation coefficient, but I have error. The only thing I can think of is that python requires different syntax.

我是 python 的新手,遇到了麻烦。我应该计算皮尔逊相关系数,但我有错误。我唯一能想到的是python需要不同的语法。

import pandas
import numpy as np

data = pandas.read_csv('One_imortant_table.csv', index_col='Id')
corr1 = data['Numb'].corr(method='pearson', min_periods=1)
print(corr1)

TypeError:

类型错误:

--->  corr1 = data['Numb'].corr(method='pearson', min_periods=1)        
TypeError: corr() missing 1 required positional argument: 'other'

What am I doing wrong here? I have searched for this mistake, but can't find any more. I am using windows 10 with the latest version of python, coding in Jupyter.

我在这里做错了什么?我已经搜索了这个错误,但找不到更多。我正在使用 Windows 10 和最新版本的 python,在 Jupyter 中编码。

回答by Miriam Farber

Correlation should be calculated between two columns. The argument 'other' is the second column that should be used. For example, you could do something like that:

应计算两列之间的相关性。参数“other”是应该使用的第二列。例如,您可以执行以下操作:

import pandas
import numpy as np

data = pandas.DataFrame({'Numb':[1,2,3],'a':[7,8,9]})
corr1 = data['Numb'].corr(data['a'],method='pearson', min_periods=1)
print(corr1)

This prints 1.0

这打印 1.0

回答by Allen

You need at least 2 columns to calculate correlation. Try this to see if it works?

您至少需要 2 列来计算相关性。试试这个,看看它是否有效?

data.corr(method='pearson', min_periods=1)