Python错误:ValueError:长度不匹配:预期轴有4个元素,新值有5个元素

时间:2018-10-05 18:16:02

标签: python pandas

这是我的下面的代码。

我想打开文件夹中的每个报告,然后将需要的所有内容复制到新报告中。

但是错误提示:

from math import tan, cos, pi, sqrt, sin, atan

def f(x,b):
    return sin(x)/x+b*cos(x)

def f1(x,b):
    return cos(x)/x-(b+x**-2)*sin(x)

e = 1e-12

def newtons_method(x0, f, f1, e):
    x0 = float(x0)
    while True:
        x1 = x0 - (f(x0,b) / f1(x0,b))
        if abs(x1 - x0) < e:
            return x1
        x0 = x1

result = []
n = int(input("Input n: "))
b = float(input("Input b: "))
for i in range(n):
    k=i; 
    if b >= -1: k=k+1
    x0 = pi/2*sqrt(1+1/b) if k==0 else  k*pi-atan(b*k*pi)
    result.append(newtons_method(x0, f , f1, e))
lambda_result = sorted(list(set(result)))
print(len(result), len(lambda_result))

有人可以帮忙吗?

PORT_result("Institution_MTD_9_28", "G:\Risk\Attribution\Fixed_Income\PORT attribution", "Daily_Performance_9_30.xlsx")

Traceback (most recent call last):

  File "<ipython-input-2-c886cd1d12e8>", line 1, in <module>
    PORT_result("Institution_MTD_9_28", "G:\Risk\Attribution\Fixed_Income\PORT attribution", "Daily_Performance_9_30.xlsx")

  File "G:/Risk/Attribution/Fixed_Income/PORT attribution/PORT comparison.py", line 49, in PORT_result
    dt.columns = range(5)

  File "C:\Users\pjia\AppData\Local\Continuum\anaconda2\lib\site-packages\pandas\core\generic.py", line 4385, in __setattr__
    return object.__setattr__(self, name, value)

  File "pandas\_libs\properties.pyx", line 69, in pandas._libs.properties.AxisProperty.__set__

  File "C:\Users\pjia\AppData\Local\Continuum\anaconda2\lib\site-packages\pandas\core\generic.py", line 645, in _set_axis
    self._data.set_axis(axis, labels)

  File "C:\Users\pjia\AppData\Local\Continuum\anaconda2\lib\site-packages\pandas\core\internals.py", line 3323, in set_axis
    'values have {new} elements'.format(old=old_len, new=new_len))

ValueError: Length mismatch: Expected axis has 4 elements, new values have 5 elements

0 个答案:

没有答案