ValueError:长度不匹配:预期轴有2个元素,新值有1个元素

时间:2019-01-22 07:05:06

标签: python pandas exception

我试图在这里为我的代码创建一个GUI。 它基本上从用户那里获得一个文件夹位置,其中包含一堆Excel文件。然后,它将所有这些文件中的数据提取并编译成我需要的格式,并将其写入输出excel文件中。

直到我决定将Tkinter用于GUI以便学习如何在该过程中使用Tkinter之前,这样的代码一直在起作用。

我试图用一个处理数据的所有不同函数来定义一个类。

但是看起来这些功能都无法相互调用,也无法调用变量,而且我不断收到像下面这样的奇怪错误。

import pandas as pd
import os
from tkinter import *
import tkinter.messagebox
import tkinter.filedialog

class specomp:
def __init__(self,master):
    frame=Frame(master)
    frame.pack()

    self.printButton=Button(frame,text="Select folder",command=self.set_thisdir)
    self.printButton.pack(side=LEFT)

    self.quitButton=Button(frame,text="Quit",command=master.destroy)
    self.quitButton.pack(side=LEFT)

    self.exButton=Button(frame,text="Execute",command=self.myprog)
    self.exButton.pack(side=LEFT)

def set_thisdir(self):
    global thisdir
    thisdir= filedialog.askdirectory(initialdir=os.getcwd(),title='Please select the folder where you saved Spectrometer excel files')


def fsheet_DCOP(j,k,l):
    #from the input frame j it returns the column that matches DCOP name in list k in column 5 and parameter name l in column 7
    ret=pd.DataFrame([])
    for i in k:
        ret=ret.append(j[j[5] == i])
    ret=ret[ret[7]==l]
    return ret

def fsheet_Table(j,k):
    #transforms the raw output of above function into final table for every file, with same column name etc.
    j=j.transpose()
    j=j[5:]
    j.columns=[k]
    j=j.transpose()
    ret=wfrpair.append(j)
    ret=ret.iloc[:,5:30]
    ret = ret.transpose()
    return ret

def fin_col_ren(j,k):
    #after all files input has been appended, this function renames all the columns, and adds the Inline parameter name to the data column
    j.columns=['MEMS Lot ID','MEMS Wfr ID','MEMBRANE Lot ID','MEMBRANE Wfr ID','TSV Lot ID','TSV Wfr ID',k]
    return j

def myprog(self):
    os.chdir(thisdir)
    print("You have following files in the target folder:\n")
    msg="You have following files in the target folder:\n"

    for i in os.listdir(os.chdir(thisdir)):
        print (i,'\n')
        msg=msg+"\n"+i
    msg=msg+"\n"
    tkinter.messagebox.showinfo("Spectrometer File Compiler", msg)


    input("Press Enter to continue...")    
    writer = pd.ExcelWriter(r'C:\Users\spoikayi\f_output.xlsx')
    spedf = pd.DataFrame([])
    pairingdf2=pd.DataFrame([])

    M32FinCDsheet=pd.DataFrame([])
    M32DICDsheet=pd.DataFrame([])
    TSVFinCDsheet=pd.DataFrame([])
    for i in os.listdir(os.chdir(thisdir)):
        fullsheet = pd.read_excel(i, sheet_name='Sheet1')
        fsheet = pd.read_excel(i, sheet_name='Sheet1',header=None)
        fsheet=fsheet.drop([0,1,2,3,4,5],axis=0)
        fsheet=fsheet.drop(columns=[0,1,2,8,9,10,11,12,13,14,15,16,42,43])
        wfrpair=fsheet.iloc[0:6]
        pairing=fullsheet.iloc[5:11,17:39]
        spe=fullsheet.iloc[[5,6,7,8,9,10,11,14,15,16,17,18,19,20],[17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39]]
        pairing=pairing.transpose()
        spe=spe.transpose()
        spedf=spedf.append(spe)
        pairingdf2=pairingdf2.append(pairing)


        df1y632=fsheet_DCOP(fsheet,['1Y632'],'Moyenne tr CD Finale-Item')
        dfTSV=fsheet_DCOP(fsheet,['1L417','1V417'],'Moyenne tr CD Finale-Item')
        M32data=fsheet_Table(df1y632,'M32_Fin_CD')
        TSVCDdata=fsheet_Table(dfTSV,'TSV_CD')
        M32FinCDsheet=M32FinCDsheet.append(M32data)
        TSVFinCDsheet=TSVFinCDsheet.append(TSVCDdata)



    spedf=spedf.drop(columns=[11,20])
    spedf.columns=['MEMS Lot ID','MEMS Wfr ID','MEMBRANE Lot ID','MEMBRANE Wfr ID','TSV Lot ID','TSV Wfr ID','VERTICAL GAP HEIGHT (M34)','MEMBRANE THICKNESS','SI GAP WIDTH (M32 FIN)','MEMBRANE TO ELECTRODE GAP','TOP CAVITY DEPTH (M94)','CONTACT ANGLE OF FOTS COATING']
    pairingdf2.columns=['MEMS Lot ID','MEMS Wfr ID','MEMBRANE Lot ID','MEMBRANE Wfr ID','TSV Lot ID','TSV Wfr ID']

    M32FinCDsheet=fin_col_ren(M32FinCDsheet,'M32 Fin CD 1Y632')
    TSVFinCDsheet=fin_col_ren(TSVFinCDsheet,'TSV Fin CD')

    pairingdf2.to_excel(writer,sheet_name='Pairing')
    spedf.to_excel(writer,sheet_name='SPE')
    M32FinCDsheet.to_excel(writer,sheet_name='M32 Fin CD 1Y632')
    TSVFinCDsheet.to_excel(writer,sheet_name='TSV Fin CD')

    writer.save()
    tkinter.messagebox.showinfo("Spectrometer File Compiler", "Compilation Complete! \nPlease Check output file.")
    return

root= tkinter.Tk()
b=specomp(root)
root.mainloop()

起初,我遇到诸如未定义thisdir的错误,因此我将其设置为全局变量。

这是我现在遇到的错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\spoikayi\Documents\Portables\Anaconda\anaconda3\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:/Users/spoikayi/.spyder-py3/Spectrometer_Compiler_GUI.py", line 100, in myprog
    M32data=fsheet_Table(df1y632,'M32_Fin_CD')
  File "C:/Users/spoikayi/.spyder-py3/Spectrometer_Compiler_GUI.py", line 45, in fsheet_Table
    j.columns=[k]
  File "C:\Users\spoikayi\Documents\Portables\Anaconda\anaconda3\lib\site-packages\pandas\core\generic.py", line 4389, in __setattr__
    return object.__setattr__(self, name, value)
  File "pandas\_libs\properties.pyx", line 69, in pandas._libs.properties.AxisProperty.__set__
  File "C:\Users\spoikayi\Documents\Portables\Anaconda\anaconda3\lib\site-packages\pandas\core\generic.py", line 646, in _set_axis
    self._data.set_axis(axis, labels)
  File "C:\Users\spoikayi\Documents\Portables\Anaconda\anaconda3\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 2 elements, new values have 1 elements

0 个答案:

没有答案