2013-07-17 43 views
1

我正在將csv文件讀入數據框,然後使用數據硝基允許用戶根據excel單元中的輸入修改數據。這工作正常,除非它似乎當一個df列中的每個值是NaN。第一步是讓用戶輸入他希望訪問數據的實體的UID。用UID作爲索引讀取csv。缺少值的DataFrame列將不會接受輸入

這是代碼:

class InterAction: 
    def __init__(self) : 
     self.PD_CL = pd.read_csv(r"C:\Users\rcreedon\Desktop\DataInProg\ContactList.csv", index_col = 'UID') 

    def CheckCL_UID(self): 
     self.UID = str(CellVal) 
     if self.UID in self.PD_CL.index.values: 
      return 'True' 
     else: 
      return "ERROR, the Factory Code you have entered is not in the Contact List" 

    def UpdateContactDetails(self, Cell_GMNum, Cell_CNum, Cell_GMNam, Cell_CNam, Cell_GMDesig, Cell_CDesig): 


     if not Cell_GMNum.is_empty(): 
      self.PD_CL['Cnum_gm'][self.UID] = str(Cell_GMNum.value) 

     if not Cell_CNum.is_empty(): 
      self.PD_CL['Cnum_upd'][self.UID] = str(Cell_CNum.value) 

     if not Cell_GMNam.is_empty(): 
      self.PD_CL['Cnam_gm'][self.UID] = str(Cell_GMNam.value) 

     if not Cell_CNam.is_empty(): 
      self.PD_CL['Cnam_upd'][self.UID] = str(Cell_CNam.value) 

     if not Cell_GMDesig.is_empty(): 
      self.PD_CL['Cdesig_gm'][self.UID] = str(Cell_GMDesig.value) 

Inter = InterAction() 
Cell("InputSheet", 5, 2).value = Inter.CheckCL_UID() 
Inter.UpdateContactDetails(Cell("InputSheet", 3, 7), Cell("InputSheet",4, 7), Cell("InputSheet",5, 7), Cell("InputSheet",6, 7), Cell("InputSheet", 7, 7), Cell("InputSheet",8, 7)) 

隨着「MP01」,這是在CSV數據幀索引當我跑這我接收復合誤差與所述GMDesig細胞相對於用戶輸入的UID 。它結束於

ValueError ['M''P''0''1']不包含在索引中。

我注意到excel文件中的CDesig_gm列是唯一沒有值的列,因此被讀入數據框中作爲NaN列。當我向csv中的某個單元格添加無意義的值並重新運行程序時,它工作正常。

這裏發生了什麼,我很難過。

謝謝

回答

1

當您嘗試更改列值時,您可能會收到TypeError。添加到您的代碼:

if not Cell_GMDesig.is_empty(): 
     self.PD_CL['Cdesig_gm'] = self.PD_CL['Cdesig_gm'].astype(str) 
     # cast to string first 
     self.PD_CL['Cdesig_gm'][self.UID] = str(Cell_GMDesig.value) 

(更多的細節:當熊貓在CSV讀取,它選擇爲每列數據類型的空白列作爲花車的列讀,寫一個字符串。其中一個條目將失敗。

在垃圾數據把讓大熊貓知道該列不應該是數值,所以寫入成功。)

+0

感謝本,你想這將是更好的申報每一列當我讀取csv時應該是一個字符串作爲字符串?因爲它是任何/所有的列可以是空的,當我讀csv熊貓。 –

+1

是的,實際上將整個數據框聲明爲字符串數據似乎在做自己的把戲self.PD_CL = self.PD_CL.astype(str) –