rename all the values in a Pandas column using an iteration (a for loop)
If there is default RangeIndex
is possible use:
df['animals'] = 'animal_' + (df.index + 1).astype(str)
Or use list comprehension with f-strings:
df['animals'] = [f'animal_{i + 1}' for i in df.index]
For any index:
df['animals'] = [f'animal_{i + 1}' for i in np.arange(len(df.index))]
EDIT: If need same number for each type of animal use:
df['new'] = [f'animal_{i + 1}' for i in pd.factorize(df['animals'])[0]]
print (df)
animals new
0 dog animal_1
1 cat animal_2
2 pig animal_3
3 dog animal_1
4 dog animal_1
5 pig animal_3