0

I have the following df:

list_columns = ['weight', 'animal', 'age']
list_data = [
    [14, '2. D', 10],
    [4, 'DOG2-', 2],
    [7, 'DOG1-', 3],
    [6, 'DOG1', 5]
    ]
df_animals = pd.DataFrame(columns=list_columns, data=list_data)
df_animals.head()

I want to check if the following values '2. D','DOG2-','DOG1-','dddog' exists in my df, and if so, to repalce them with the string 'dog1'

My tries:

For just one value I can do this:

if 'DOG2-' in df_animals.values:
    df_animals['animal'] = df_animals['animal'].replace({'DOG2-':'dog1'})

I tought using a list but doesen't work because of the signs - .. Witought them it does the job.

list = ('2. D','DOG2-','DOG1-','DOG-')
if list in df_animals.values:
    df_animals['animal'] = df_animals['animal'].replace({'2. D':'DOG2','DOG2-':'DOG2','DOG1-':'DOG1','DOGG 1':'DOG1'})

This just returns the same df with the warning: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison

I am now thinking to use python’s RegEx (re) library but it would be to many lines and don't know if it's the most pythonic way. With the list seems simple but I think I am missing somethig. Could someone help me with an idea about my issue?

Christian
  • 459
  • 4
  • 12
  • `df.replace(('2. D','DOG2-','DOG1-','DOG-'), 'blah')` you can replace multiple values. – cs95 Dec 16 '19 at 07:43
  • I was sure it had to be something simple. Thank you @cs95 . Please post this as a question. I will accept the answer and maybe could also help others. – Christian Dec 16 '19 at 07:46
  • Thanks but this is a duplicate question. You can vote on [my answer](https://stackoverflow.com/a/46920621/4909087) to the duplicate target if you wish. Thanks :-) – cs95 Dec 16 '19 at 07:51

0 Answers0