replace panda’s dataframe column values based on containing dictionary keys

You could use DataFrame.replace with regex parameter set to True and pass the mapping dictionary.

df.replace(dictionary, regex=True)

#   col2
# 0    5
# 1  abc
# 2    8

This usage of df.replace is less known. You can read more about it here.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top