python - How to replace multiple values in a pandas dataframe? -


how can replace multiple values mapping in pandas dataframe?

i have 1 column want replace following mapping.

mapping apple=fruit tomato=vegetable steak=protein milk=dairy 

so column becomes.

col1       ->     col1 apple             fruit apple             fruit tomato            vegtable steak             protein milk              dairy 

how can efficiently?

just create dictionary mapping , call series.map.

>>> d = {'apple': 'fruit',           'tomato': 'vegetable',           'steak': 'protein',           'milk': 'dairy'}  >>> df.col1.map(d)  0        fruit 1        fruit 2    vegetable 3      protein 4        dairy name: col1, dtype: object 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -