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?