user2866103
user2866103

Reputation: 10555

KeyError: u'no item named <name>'

I have this function which places data into another dataframe.

def set_var(self,navn,periode,value):
    try:
        if navn.upper() not in self.data:
            self.data[navn.upper()]=NaN
        self.data[navn.upper()].ix[periode]=value
    except:
        print('> fejl ved genmning af:',navn,'i',periode)
        print( self.data[navn.upper()])
    return

which I then iterate like this, this fill with data:

for idx, val in enumerate(bd):
    if val not in ('Navn','Type','Aarstal','Referenceperiode','Registreringsnummer','nummer','navn','navn1','period', 'regnr'):
        for row in bd.itertuples():
            banker.set_var((str(row[6])+'_'+str(val)),row[60], row[idx+1])

I get the error: KeyError: u'no item named <name of first item I try to insert>'

Why is this?

Upvotes: 1

Views: 14092

Answers (1)

nitin
nitin

Reputation: 7358

A Key Error is typically because the dict object does not have the key you are requesting. It seems to me that self.data is a dict. If so, you could test your object by using has_key()... like so,

if self.data.has_key(navn.upper()):
        self.data[navn.upper()]=NaN

Upvotes: 2

Related Questions