0

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.)

dstrants
  • 5,645
  • 2
  • 18
  • 27
  • 1
    Instead of the for loop, you can just use `df['outcome_type'] = (df['outcome_type']=='Adoption').astype(int)` – Craig Jul 14 '20 at 02:16

1 Answers1

1

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?

sashimi
  • 1,078
  • 2
  • 12
  • 21