pourya-ir/Medium

Overlapping keys in dictionary when Using .replace() method on pandas dataframe

Opened this issue · 0 comments

Hey,

thanks for your code. I used it an ran into a problem. In the Test class.
I changed the replace with map and it works again.

class KFoldTargetEncoderTest(base.BaseEstimator, base.TransformerMixin):
    def __init__(self,train,colNames,encodedName):
        self.train = train
        self.colNames = colNames
        self.encodedName = encodedName
        
    def fit(self, X, y=None):
        return self

    def transform(self,X):

        mean = self.train[[self.colNames,self.encodedName]].groupby(self.colNames).mean().reset_index() 
        
        dd = {}
        for index, row in mean.iterrows():
            dd[row[self.colNames]] = row[self.encodedName]

        X[self.encodedName] = X[self.colNames]
        X[self.encodedName] = X[self.encodedName].map(dd)

        return X