首页 文章

具有缺失值的DataFrame列将不接受输入

提问于
浏览
1

我正在将csv文件读入数据框,然后使用数据nitro允许用户根据excel单元格中的输入修改数据 . 这工作正常,但似乎df列中的每个值都是NaN . 第一步是用户输入他希望访问数据的实体的UID . 以UID作为索引读取csv .

这是代码:

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))

UID为'MP01',它位于csv数据帧索引中当我运行它时,我收到了关于GMDesig单元格中用户输入的复合错误 . 结束了

ValueError ['M''P''0''1']未包含在索引中 .

我注意到excel文件中的CDesig_gm列是唯一没有值的列,因此作为一列NaN读入数据框 . 当我向csv中的一个单元格添加一个无意义的值并重新运行程序时,它运行正常 .

这里发生了什么,我很难过 .

谢谢

1 回答

  • 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)
    

    (更多细节:当Pandas读入CSV时,它会为每列选择一种数据类型 . 空行列作为浮点列读入,将字符串写入其中一个条目将失败 .

    放入垃圾数据可以让pandas知道列不应该是数字,因此写入成功 . )

相关问题