gpt4 book ai didi

python - 缺少值的 DataFrame 列将不接受输入

转载 作者:太空宇宙 更新时间:2023-11-03 18:53:56 26 4
gpt4 key购买 nike

我正在将 csv 文件读入数据框中,然后使用 data ninto 允许用户根据 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 中的一个单元格添加一个无意义的值并重新运行该程序时,它工作正常。

这里发生了什么,我很困惑。

谢谢

最佳答案

当您尝试更改列值时,可能会收到类型错误。将其添加到您的代码中:

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 时,它会为每一列选择一种数据类型。空白列将作为一列 float 读入,向其中一个条目写入字符串将会失败。

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

关于python - 缺少值的 DataFrame 列将不接受输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17692948/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com