Home > Mobile >  Changing a column value in pandas if value in other column matches string
Changing a column value in pandas if value in other column matches string

Time:11-02

So, I'm a beginner to Pandas and I'm having a little trouble getting something to work.

My task is that I was given a dataframe as follows:

AnimalID Pet Name Pet Type
1 Whiskers Cat
2 Charlie Dog
3 Spot Dog
4 Honey Cat

What I'm attempting to do is add a ' ' symbol to the end of the ID if the pet type is a dog.

My solution was to iterate through each row using iterrows() and check if the Pet Type Matches, and if it does, change the ID in that row. My code is below:

import pandas as pd
#df = pd.read_excel('AnimalList.xlsx')
df = pd.DataFrame({'AnimalID':[1,2,3,4], 'Pet Name':['Whiskers', 'Charlie', 'Spot', 'Honey'], 'Pet Type':['Cat', 'Dog', 'Dog', 'Cat']})
for row in df.iterrows():
    if row['Pet Type'].eq('Dog').any():
        row['AnimalID'] = df['AnimalID'].astype(str)   ' '

df

The dataframe is printing without error, but is there any reason that nothing is changing? I have to be missing something obvious. Thank you.

CodePudding user response:

You can apply Series.where to column AnimalID. Its values will be replaced where the condition is False, so we will need to check Series.ne to add for each row where this column equals Dog.

df['AnimalID'] = df['AnimalID'].where(df['Pet Type'].ne('Dog'), 
                                      df['AnimalID'].astype(str)   ' ')

print(df)

  AnimalID  Pet Name Pet Type
0        1  Whiskers      Cat
1       2    Charlie      Dog
2       3       Spot      Dog
3        4     Honey      Cat

CodePudding user response:

here is one way to do it using mask

# when condition is true, add   to the ID
df['AnimalID']=(df['AnimalID'].mask(df['Pet Type'].eq('Dog'), 
                                    df['AnimalID'].astype(str)   (' ')))
df
    AnimalID    Pet Name    Pet Type
0          1    Whiskers    Cat
1         2     Charlie     Dog
2         3     Spot        Dog
3          4    Honey       Cat
  • Related