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
Post a Comment