1

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?

1
  • 1
    replace is not an inplace operation by default. You must assign the changes back to the column of interest. So - mapped[coln].replace(str(val), str(map), inplace=True) Commented Jan 5, 2017 at 14:40

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.