SaikiHanee
SaikiHanee

Reputation: 881

replace values in a python dataframe

I am trying to replace values in the 'mapped' dataframe with mappings in 'mappings' dataframe.

for column in df:
    mapped[column] = df[column].astype(str)

for i, row in mappings.iterrows():
    coln = row['colname']
    val = row['value']
    map = row['mapping']
    print 'match::' + coln + ":"+str(val)+ ":"+str(map)
    print mapped[mapped[coln]== val]
    mapped[coln].replace(str(val), str(map))

print mapped.head()

Although there are matching records, the values in 'mapped' dataframe is not getting replaced. How can I fix this?

Upvotes: 1

Views: 263

Answers (1)

DeepSpace
DeepSpace

Reputation: 81684

mapped[coln].replace(str(val), str(map))

replace is not inplace by default. Either pass it inplace=True or reassign it:

mapped[coln].replace(str(val), str(map), inplace=True)

or

mapped[coln] = mapped[coln].replace(str(val), str(map))

Upvotes: 2

Related Questions