1

How can I replace multiple values with a mapping in a Pandas DataFrame?

I have one column that I want to replace with the following mapping.

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

So that my column becomes.

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

How can I do this most efficiently?

1 Answer 1

2

Just create a dictionary with your mapping and then 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
Sign up to request clarification or add additional context in comments.

3 Comments

I tired using the code df[col1]=df.col1.map(d) and i got an error. A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
@OptimusPrime This is because of a step you must be taking before. Probably subsetting the columns of your DataFrame, doing something like df = df[['col1', 'col2']]. Use df.loc or df.iloc when subsetting columns instead, so that when you go to set a column like this you won't run into problems.
can you give me an example? of using loc or iloc? thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.