I'm reading a csv file into a data frame, and then using data nitro to allow users to modify the data based on inputs in excel cells. This works fine except it seems when every value in a df column is NaN. The first step is for the user to enter the UID of the entity for which he wishes to access the data. The csv is read with the UIDs as index.

This is the code:

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

With a UID of 'MP01' which is in the csv dataframe index When I ran this I was receiving a composite error with respect to the user input in the GMDesig cell. It ended in

ValueError ['M' 'P' '0' '1'] not contained in index.

I noticed that the CDesig_gm column in the excel file was the only column with no values and was consequently read into the data frame as a column of NaNs. When I added a meaningless value to one of the cells in the csv and re-ran the program it worked fine.

What is happening here, I'm stumped.

Thanks

有帮助吗?

解决方案

You might be getting a TypeError when you're trying to change the column value. Add this to your code:

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)

(Some more details: When Pandas reads in a CSV, it selects a datatype for each column. A blank column is read in as a column of floats, and writing a string to one of the entries will fail.

Putting in junk data lets pandas know that the column shouldn't be numerical, so the write succeeds.)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top