Replacing data frame column categories with 0 and 1

The column outcome_type in my data frame (far right in image) contains 9 categories of what happens to animals at a shelter (adoption, died, transferred, etc). I am trying to change those categories to be 1 for Adoption and 0 for all other categories. I tried to do so using this for loop, but when I print the df afterward, the categories are still there and have not been replaced with 0 and 1. Can anyone tell me why? enter image description here

import pandas as pd df = pd.read_csv('aac_shelter_outcomes.csv') df.head()    for item in df['outcome_type']:     if item == "Adoption":         item = int('1')     else: item = int('0')          

EDIT: I realize there may be another way to do this that someone already discovered in another question, but I would like to know WHY my for loop approach does not work. (I am a beginner with python and am looking to learn from this. I can’t see why the for loop doesn’t change the df values.)

Add Comment
1 Answer(s)

The following should solve the issue: df.loc[df[‘outcome_type’] =’Adoption’, ‘outcome_type’] = 1

If you need a one hot encoding, you may find details here: How can I one hot encode in Python?

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.