[Python] > AttributeError: 'float' object has no attribute 'items'

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    I've read many posts on this site but not yet posted so I apologize for first-timer mistakes.

    I'm writing a class that has created a pandas dataframe to store information about an instance of the class.

    I call the dataframe _gamestats. Its index was a column called 'faces' and the one column is the values, or the 'weights'. I was not having any success trying to alter the value in the dataframe. I need to write a method to alter the value of the 'weight' of one of the 'faces.'

    To do this I though I could convert the dataframe to a dictionary, use the key to alter the value and then reconvert to a dataframe. However, I'm getting an error message that I do not understand.


    AttributeError: 'float' object has no attribute 'items'
    ​


    # method: change the weight of a single side:

    def change_facewt(self, face, nwt):
    self.nwt = nwt
    self.face = face
    x = len(self.faces)

    # check if nwt is int or float - OR castable | TypeError

    if not isinstance(nwt, (int, float)):
    try:
    nwt = float(nwt)
    except:
    raise TypeError("New weight must be numeric.")

    # change the weight of self.face with self.nwt

    stat_dict = self._gamestats.to_dict('index')
    stat_dict[self.face] = self.nwt
    self._gamestats = pd.DataFrame.from_dict(stat_dict, orient = 'index')
    print(self._gamestats())

    Continue reading...

Compartilhe esta Página